npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-ng-fixtures

v1.0.2

Published

Run your AngularJS app using mock data in place of your back-end.

Downloads

6

Readme

gulp-ng-fixtures

npm version Dependency Status

Run your AngularJS app using mock data in place of your back-end

Avoid monkey patching your AngularJS app when back-end services are unavailble or down. With gulp-ng-fixtures you can easily serve a version of your app that uses mock data in place of your back-end.

Installation

Install gulp-ng-fixtures as a development dependency:

npm install --save-dev gulp-ng-fixtures

Usage

Minimal example: in your Gulpfile.js:

Note: The html file passed to gulp.src must contain the ng-app directive for your AngularJS app.

var ngFixtures = require('gulp-ng-fixtures');

gulp.src('app/index.html')
  .pipe(ngFixtures({
    appModule: 'myapp',
    fixtures: [
      {
        req: '/users/ryandrewjohnson',
        res: { name: 'Ryan Johnson', email: '[email protected]' }
      },
      {
        req: /views\/*.html/
      }
    ]
  })
  .pipe(gulp.dest('.tmp/serve'));

Creating your fixtures

Because the plugin uses Angular's $httpBackend service all http requests will be captured by default. This means you are responsible for capturing and providing a response for each of these requests. Failure to do so will result in an error like Error: Unexpected request: GET views/main.html.

Having to create a fixture for each and every http request would be cumbersome. Good news is this is not necessary and quite easy to avoid.

Example targeting single http request

By using regex we can target all requests with a single pattern /*./ and then omit the res property to have the request passThrough.

Note: Order of fixtures is important. Any "catch all" fixtures should go at the end of the array.

ngFixtures({
  appModule: 'myapp',
  fixtures: [
    {
      req: '/users/ryandrewjohnson',
      res: { name: 'Ryan Johnson', email: '[email protected]' }
    },
    {
      req: /*./
    }
  ]
})

Removing fixtures

It is recommended that you set your gulp.dest to be different than your gulp.src since ngFixtures injects JS into the outputted html file that should not be considered permenant.

/* avoid */
gulp.src('app/index.html')
  .pipe(ngFixtures({
    ...
  })
  .pipe(gulp.dest('app'));

/* recommended */
gulp.src('app/index.html')
  .pipe(ngFixtures({
    ...
  })
  .pipe(gulp.dest('.tmp/serve'));

If you are unable to follow the recommended example ngFixtures does have an undo option that will remove all the changes made to the outputted html file. Instead of passing an options object to ngFixtures you just pass in the appModule name.

Example removing fixtures
/* add fixtures */
gulp.src('app/index.html')
  .pipe(ngFixtures({
    appModule: 'myapp'
    ...
  })
  .pipe(gulp.dest('app'));

/* remove fixtures */
gulp.src('app/index.html')
  .pipe(ngFixtures('myapp')
  .pipe(gulp.dest('app'));

Options

ngFixtures(options)

options.appModule

Type: string required

The name of your AngularJS app module which can be found in the ng-app directive of your main html file.

options.fixtures

Type: array required

An array of fixture object's where each object represents a http request you want to intercept.

Example fixtures array:
[
  {
    req: '/users/ryandrewjohnson',
    res: { name: 'Ryan Johnson', email: '[email protected]' }
  },
  {
    req: '/user/issues',
    res: '/app/fixtures/issues.json',
    method: 'POST',
    status: 200
  },
  {
    req: /views\/*.html/
  }
]

options.ngversion

Type: string Default: '1.4.8'

The AngularJS version your app is using. The plugin loads angular-mocks.js from cdnjs.com and requires the version number to load the correct file. Example of cdnjs url //cdnjs.cloudflare.com/ajax/libs/angular.js/{{version}}/angular-mocks.js where {{version}} will be replaced with options.ngversion.

ngFixtures(appModule)

This syntax is used to remove fixtures that were added to an html file by ngFixtures.

appModule

Type: string required

The name of your AngularJS app module.

Fixture Object

{
  req: '/users/ryandrewjohnson',
  res: { name: 'Ryan Johnson', email: '[email protected]' },
  method: 'POST',
  status: 200
}

req

Type: (string|RegExp) required

Will match the http request you are trying to intercept. If you pass in a string it will try to match the url exactly, where if you use a regular expression it will intercept all url's that match the pattern.

res

Type: any Default: null

The mock data you want the request to return.

Simple responses:
{ req: ..., res: 'success' }
{ req: ..., res: 500 }
{ req: ..., ['apples', 'oranges', 'pears'] }
{ req: ..., { name: 'testy', email: '[email protected]' } }
Complex responses:

For large data responses e.g. (20 user objects) you can move this data into an external .json file. Then provide the relative path to the file as the value for res. The json file will be parsed and returned as the response for the request. If you have existing json fixtures for your unit tests you can reuse them here.

The following assumes a users.json file exists in a folder called fixtures in the project root.

{
  req: 'get/users',
  res: 'fixtures/users.json'
}
PassThrough responses:

If you don't want to return any data omit the res property from your fixture object.

{
  req: /views\/*.html/
}

method

Type: string Default: GET

The http method for your request. Valid values include GET, POST, PUT, DELETE, HEAD.

status

Type: number Default: 200

The http status code for your request. Valid values include 200, 400, 401 etc..

Gotchas

  • Your AngularJS app must have a main html file with an ng-app directive.
  • Only tested in Angular 1.x