From 0a2555c14bfe0510dd2dc178f27d9b0f194a1bba Mon Sep 17 00:00:00 2001 From: Jo Pearce Date: Tue, 17 Sep 2019 14:19:03 +0100 Subject: [PATCH] fix(testing): fix the breaking jest builder spec (#1851) --- .../jest/src/builders/jest/jest.impl.spec.ts | 450 +++++++++--------- 1 file changed, 232 insertions(+), 218 deletions(-) diff --git a/packages/jest/src/builders/jest/jest.impl.spec.ts b/packages/jest/src/builders/jest/jest.impl.spec.ts index f93ee7e963dd6..1765c77666035 100644 --- a/packages/jest/src/builders/jest/jest.impl.spec.ts +++ b/packages/jest/src/builders/jest/jest.impl.spec.ts @@ -1,24 +1,24 @@ import { schema } from '@angular-devkit/core'; import { TestingArchitectHost } from '@angular-devkit/architect/testing'; - -jest.mock('jest'); -const { runCLI } = require('jest'); -const mockJestConfig: any = {}; -jest.mock('/root/jest.config.js', () => mockJestConfig, { virtual: true }); - import * as path from 'path'; import { Architect } from '@angular-devkit/architect'; describe('Jest Builder', () => { let architect: Architect; + let runCLI: jest.Mock; beforeEach(async () => { + jest.resetModules(); + + runCLI = jest.fn(); + jest.doMock('jest', () => ({ + runCLI + })); + const registry = new schema.CoreSchemaRegistry(); registry.addPostTransform(schema.transforms.addUndefinedDefaults); const testArchitectHost = new TestingArchitectHost('/root', '/root'); - mockJestConfig.globals = {}; - architect = new Architect(testArchitectHost, registry); await testArchitectHost.addBuilderFromPackage( path.join(__dirname, '../../..') @@ -33,168 +33,133 @@ describe('Jest Builder', () => { ); }); - it('should send appropriate options to jestCLI', async () => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - watch: false + describe('when the jest config file is untouched', () => { + beforeEach(() => { + jest.doMock('/root/jest.config.js', () => ({}), { virtual: true }); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - expect(runCLI).toHaveBeenCalledWith( - { - _: [], - globals: JSON.stringify({ - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - watch: false - }, - ['/root/jest.config.js'] - ); - }); - it('should send appropriate options to jestCLI when testFile is specified', async () => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - testFile: 'lib.spec.ts', - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - codeCoverage: false, - runInBand: true, - testNamePattern: 'should load', - testPathPattern: '/test/path', - colors: false, - reporters: ['/test/path'], - verbose: false, - coverage: false, - coverageReporters: 'test', - coverageDirectory: '/test/path', - watch: false + it('should send appropriate options to jestCLI', async () => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + watch: false + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); + expect(runCLI).toHaveBeenCalledWith( + { + _: [], + globals: JSON.stringify({ + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + watch: false + }, + ['/root/jest.config.js'] + ); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - expect(runCLI).toHaveBeenCalledWith( - { - _: ['lib.spec.ts'], - globals: JSON.stringify({ - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - coverage: false, + it('should send appropriate options to jestCLI when testFile is specified', async () => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + testFile: 'lib.spec.ts', + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + codeCoverage: false, runInBand: true, testNamePattern: 'should load', testPathPattern: '/test/path', colors: false, reporters: ['/test/path'], verbose: false, + coverage: false, coverageReporters: 'test', coverageDirectory: '/test/path', watch: false - }, - ['/root/jest.config.js'] - ); - }); + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); - it('should send appropriate options to jestCLI when findRelatedTests is specified', async () => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - findRelatedTests: 'file1.ts,file2.ts', - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - codeCoverage: false, - runInBand: true, - testNamePattern: 'should load', - watch: false + expect(runCLI).toHaveBeenCalledWith( + { + _: ['lib.spec.ts'], + globals: JSON.stringify({ + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + coverage: false, + runInBand: true, + testNamePattern: 'should load', + testPathPattern: '/test/path', + colors: false, + reporters: ['/test/path'], + verbose: false, + coverageReporters: 'test', + coverageDirectory: '/test/path', + watch: false + }, + ['/root/jest.config.js'] + ); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - expect(runCLI).toHaveBeenCalledWith( - { - _: ['file1.ts', 'file2.ts'], - globals: JSON.stringify({ - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - coverage: false, - findRelatedTests: true, + it('should send appropriate options to jestCLI when findRelatedTests is specified', async () => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + findRelatedTests: 'file1.ts,file2.ts', + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + codeCoverage: false, runInBand: true, testNamePattern: 'should load', watch: false - }, - ['/root/jest.config.js'] - ); - }); + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); - it('should send other options to jestCLI', async () => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - codeCoverage: true, - bail: 1, - color: false, - ci: true, - json: true, - maxWorkers: 2, - onlyChanged: true, - outputFile: 'abc.txt', - passWithNoTests: true, - silent: true, - testNamePattern: 'test', - testPathPattern: '/test/path', - colors: false, - reporters: ['/test/path'], - verbose: false, - coverage: false, - coverageReporters: 'test', - coverageDirectory: '/test/path', - updateSnapshot: true, - useStderr: true, - watch: false, - watchAll: false + expect(runCLI).toHaveBeenCalledWith( + { + _: ['file1.ts', 'file2.ts'], + globals: JSON.stringify({ + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + coverage: false, + findRelatedTests: true, + runInBand: true, + testNamePattern: 'should load', + watch: false + }, + ['/root/jest.config.js'] + ); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - expect(runCLI).toHaveBeenCalledWith( - { - _: [], - globals: JSON.stringify({ - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - coverage: true, + + it('should send other options to jestCLI', async () => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + codeCoverage: true, bail: 1, color: false, ci: true, @@ -207,91 +172,140 @@ describe('Jest Builder', () => { testNamePattern: 'test', testPathPattern: '/test/path', colors: false, - verbose: false, reporters: ['/test/path'], + verbose: false, + coverage: false, coverageReporters: 'test', coverageDirectory: '/test/path', updateSnapshot: true, useStderr: true, watch: false, watchAll: false - }, - ['/root/jest.config.js'] - ); - }); + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); + expect(runCLI).toHaveBeenCalledWith( + { + _: [], + globals: JSON.stringify({ + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + coverage: true, + bail: 1, + color: false, + ci: true, + json: true, + maxWorkers: 2, + onlyChanged: true, + outputFile: 'abc.txt', + passWithNoTests: true, + silent: true, + testNamePattern: 'test', + testPathPattern: '/test/path', + colors: false, + verbose: false, + reporters: ['/test/path'], + coverageReporters: 'test', + coverageDirectory: '/test/path', + updateSnapshot: true, + useStderr: true, + watch: false, + watchAll: false + }, + ['/root/jest.config.js'] + ); + }); - it('should send the main to runCLI', async () => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - setupFile: './test-setup.ts', - watch: false + it('should send the main to runCLI', async () => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + setupFile: './test-setup.ts', + watch: false + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); + expect(runCLI).toHaveBeenCalledWith( + { + _: [], + globals: JSON.stringify({ + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + setupFilesAfterEnv: ['/root/test-setup.ts'], + watch: false + }, + ['/root/jest.config.js'] + ); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - expect(runCLI).toHaveBeenCalledWith( - { - _: [], - globals: JSON.stringify({ - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - setupTestFrameworkScriptFile: '/root/test.ts', + + it('should return the proper result', async done => { + const run = await architect.scheduleBuilder('@nrwl/jest:jest', { + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', watch: false - }, - ['/root/jest.config.js'] - ); + }); + expect(await run.result).toEqual( + jasmine.objectContaining({ + success: true + }) + ); + done(); + }); }); - it('should merge the globals property from jest config', async () => { - mockJestConfig.globals = { hereToStay: true }; - - await architect.scheduleBuilder('@nrwl/jest:jest', { - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - setupFile: './test.ts', - watch: false + describe('when the jest config file has been modified', () => { + beforeAll(() => { + jest.doMock( + '/root/jest.config.js', + () => ({ globals: { hereToStay: true } }), + { virtual: true } + ); }); - expect(runCLI).toHaveBeenCalledWith( - { - _: [], - globals: JSON.stringify({ - hereToStay: true, - 'ts-jest': { - tsConfig: '/root/tsconfig.test.json', - stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: [ - 'jest-preset-angular/InlineHtmlStripStylesTransformer' - ] - } - }), - setupFilesAfterEnv: ['/root/test-setup.ts'], + it('should merge the globals property from jest config', async () => { + await architect.scheduleBuilder('@nrwl/jest:jest', { + jestConfig: './jest.config.js', + tsConfig: './tsconfig.test.json', + setupFile: './test-setup.ts', watch: false - }, - ['/root/jest.config.js'] - ); - }); + }); - it('should return the proper result', async done => { - const run = await architect.scheduleBuilder('@nrwl/jest:jest', { - jestConfig: './jest.config.js', - tsConfig: './tsconfig.test.json', - watch: false + expect(runCLI).toHaveBeenCalledWith( + { + _: [], + globals: JSON.stringify({ + hereToStay: true, + 'ts-jest': { + tsConfig: '/root/tsconfig.test.json', + stringifyContentPathRegex: '\\.(html|svg)$', + astTransformers: [ + 'jest-preset-angular/InlineHtmlStripStylesTransformer' + ] + } + }), + setupFilesAfterEnv: ['/root/test-setup.ts'], + watch: false + }, + ['/root/jest.config.js'] + ); }); - expect(await run.result).toEqual( - jasmine.objectContaining({ - success: true - }) - ); - done(); }); });