angular-snapshot-testing
Angular component snapshot testing for Jasmine
How to Use
- Install the extension: Via npm:
npm install jasmine-angular-snapshot-testing --save-dev
Via yarn:
yarn add jasmine-angular-snapshot-testing --dev
- Initialize the testing environment
This extension requries you to run your unit tests on node instead of in a browser.
This requires
@angular/platform-server
:
;; getTestBed.initTestEnvironment ServerTestingModule, platformServerTesting;
- Configure the extension: After initializing the test environment initialize the snapshot testing extension:
; initializeSnapshots;
- Use beforeAll/afterAll hooks to load and save snapshots: This must be done in each spec file:
; describe'Table Component',;
- Generate snapshots:
Use the new
toMatchSnapshot()
matcher to generate snapshots for any object that implements.toJSON()
. Note that this module adds a.toJSON()
method to component fixtures:
; beforeEach); it'should compile successfully',;
Caveats
This module has a number of caveats that restrict the type of components that can be tested with snapshots:
-
Unit tests must be run on node instead of a browser. This is a requirement because this extension requires access to the filesystem
-
Components cannot make use of external stylesheets or templates. This is because
platform-server
does not implement aResourceLoader
, though one may be able to be written.