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"
}
@lionelB
Copy link

lionelB commented May 24, 2017

@Swiip I'm trying to migrate from jasmine to jest too but I couldn't find how to setup test properly. Initially, we ran test with the generated bundle. Could you point me to a small test case to see how import and test are written in the test ?

@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