Skip to content

Instantly share code, notes, and snippets.

@Swiip
Last active June 2, 2020 11:52
Show Gist options
  • Save Swiip/d1079f4cdcfb56967ba61af12dcfa738 to your computer and use it in GitHub Desktop.
Save Swiip/d1079f4cdcfb56967ba61af12dcfa738 to your computer and use it in GitHub Desktop.
"Testing an AngularJS app with Jest" jest code samples
var localStorageMock = /* ... some mock code ... */
Object.defineProperty(window, 'localStorage', {
value: localStorageMock
});
import './app/index.module';
import 'angular-mocks';
import './app/mock/utils';
"jest": {
"testPathDirs": ["src"],
"setupTestFrameworkScriptFile": "src/jest.init.js"
}
@Swiip
Copy link
Author

Swiip commented Sep 5, 2017

@lionelB Totally missed the notif sorry. If still relevant, here is some pseudo code of a test. Unfortunatly we had some factorization on mocking Angular injection.

import { ProductController } from './product.controller';

describe('controller: ProductController', () => {
  beforeEach(mock.controller('ProductController', ProductController, ['pstates', '$ngRedux']));

  beforeEach(function () {
    this.scope = this.$rootScope.$new();
    this.controller = this.$controller('ProductController', { $scope: this.scope });
    this.scope.product = this.controller;
    this.controller.$onInit();
  });

  it('should dispatch `LOAD_PRODUCT` action', function () {
    expect(this.productActions.loadProduct).toHaveBeenCalledWith('p1');
  });
});

@yohodopo
Copy link

yohodopo commented Mar 3, 2019

Hey @Swiip, can you please share some light on the factorization on mocking Angular injection you've done here?.

@WuglyakBolgoink
Copy link

Hallo @Swiip can you share a full project to check configs and creating bundles? I'm stucked now in my project by integration jest with ng1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment