From 29074dd8b9b5a5afdcbd006433a1d86be37d43f0 Mon Sep 17 00:00:00 2001 From: Ahn Date: Thu, 5 Nov 2020 10:46:23 +0100 Subject: [PATCH] test(unit): add mock data, rename tests (#610) Separate from #562 --- jest.config.js | 2 +- src/__tests__/__mocks__/app.component.ts | 10 ++++++++ src/__tests__/__mocks__/foo.component.ts | 9 ++++++++ src/__tests__/__mocks__/foo.service.ts | 9 ++++++++ src/__tests__/__mocks__/foo.spec.ts | 5 ++++ src/__tests__/__mocks__/foo.ts | 3 +++ ...test.ts.snap => inline-files.spec.ts.snap} | 0 .../__snapshots__/ng-jest-config.spec.ts.snap | 23 ++++++++++++++++--- ...test.ts.snap => strip-styles.spec.ts.snap} | 0 ...ine-files.test.ts => inline-files.spec.ts} | 0 src/__tests__/json.spec.ts | 16 ------------- src/__tests__/ng-jest-config.spec.ts | 16 ++++++------- ...adata.test.ts => reflect-metadata.spec.ts} | 0 ...ip-styles.test.ts => strip-styles.spec.ts} | 0 src/__tests__/tsconfig-ve.json | 12 ---------- tsconfig.spec.json | 5 +++- 16 files changed, 68 insertions(+), 42 deletions(-) create mode 100644 src/__tests__/__mocks__/app.component.ts create mode 100644 src/__tests__/__mocks__/foo.component.ts create mode 100644 src/__tests__/__mocks__/foo.service.ts create mode 100644 src/__tests__/__mocks__/foo.spec.ts create mode 100644 src/__tests__/__mocks__/foo.ts rename src/__tests__/__snapshots__/{inline-files.test.ts.snap => inline-files.spec.ts.snap} (100%) rename src/__tests__/__snapshots__/{strip-styles.test.ts.snap => strip-styles.spec.ts.snap} (100%) rename src/__tests__/{inline-files.test.ts => inline-files.spec.ts} (100%) delete mode 100644 src/__tests__/json.spec.ts rename src/__tests__/{reflect-metadata.test.ts => reflect-metadata.spec.ts} (100%) rename src/__tests__/{strip-styles.test.ts => strip-styles.spec.ts} (100%) delete mode 100644 src/__tests__/tsconfig-ve.json diff --git a/jest.config.js b/jest.config.js index 26ef68d12e..bda8709ffc 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,5 +7,5 @@ module.exports = { tsconfig: 'tsconfig.spec.json', }, }, - testPathIgnorePatterns: ['/e2e/'], + testPathIgnorePatterns: ['/e2e/', '/src/__tests__/__mocks__/'], }; diff --git a/src/__tests__/__mocks__/app.component.ts b/src/__tests__/__mocks__/app.component.ts new file mode 100644 index 0000000000..db49a17c2a --- /dev/null +++ b/src/__tests__/__mocks__/app.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.scss'], +}) +export class AppComponent { + title = 'test-app-v10'; +} diff --git a/src/__tests__/__mocks__/foo.component.ts b/src/__tests__/__mocks__/foo.component.ts new file mode 100644 index 0000000000..4290a3cdf1 --- /dev/null +++ b/src/__tests__/__mocks__/foo.component.ts @@ -0,0 +1,9 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'foo-root', + template: `

Hello World !!

`, +}) +export class FooComponent { + title: number = 'test-app-v10'; +} diff --git a/src/__tests__/__mocks__/foo.service.ts b/src/__tests__/__mocks__/foo.service.ts new file mode 100644 index 0000000000..bed2626d6f --- /dev/null +++ b/src/__tests__/__mocks__/foo.service.ts @@ -0,0 +1,9 @@ +import { Injectable } from '@angular/core'; + +export class ClassInject {} + +@Injectable() +export class MyService { + // eslint-disable-next-line + constructor(_v: ClassInject) {} +} diff --git a/src/__tests__/__mocks__/foo.spec.ts b/src/__tests__/__mocks__/foo.spec.ts new file mode 100644 index 0000000000..5106dcfdd9 --- /dev/null +++ b/src/__tests__/__mocks__/foo.spec.ts @@ -0,0 +1,5 @@ +import { getFoo } from './foo'; + +jest.mock('./foo'); + +console.log(getFoo()); diff --git a/src/__tests__/__mocks__/foo.ts b/src/__tests__/__mocks__/foo.ts new file mode 100644 index 0000000000..d39beb2f5c --- /dev/null +++ b/src/__tests__/__mocks__/foo.ts @@ -0,0 +1,3 @@ +export function getFoo(): string { + return 'foo'; +} diff --git a/src/__tests__/__snapshots__/inline-files.test.ts.snap b/src/__tests__/__snapshots__/inline-files.spec.ts.snap similarity index 100% rename from src/__tests__/__snapshots__/inline-files.test.ts.snap rename to src/__tests__/__snapshots__/inline-files.spec.ts.snap diff --git a/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap b/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap index c1655b663f..b06b5dffb1 100644 --- a/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap +++ b/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap @@ -2,11 +2,28 @@ exports[`NgJestConfig readTsConfig should return config including Angular compiler config with tsconfig as a string from ts-jest option 1`] = ` Object { - "enableIvy": false, - "fullTemplateTypeCheck": true, + "enableIvy": true, + "esModuleInterop": true, + "experimentalDecorators": true, + "forceConsistentCasingInFileNames": true, + "importHelpers": true, + "importsNotUsedAsValues": 2, + "lib": Array [ + "lib.esnext.d.ts", + "lib.dom.d.ts", + ], "module": 1, - "strictInjectionParameters": true, + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "skipLibCheck": true, + "strict": true, "target": 1, + "types": Array [ + "node", + "jest", + ], } `; diff --git a/src/__tests__/__snapshots__/strip-styles.test.ts.snap b/src/__tests__/__snapshots__/strip-styles.spec.ts.snap similarity index 100% rename from src/__tests__/__snapshots__/strip-styles.test.ts.snap rename to src/__tests__/__snapshots__/strip-styles.spec.ts.snap diff --git a/src/__tests__/inline-files.test.ts b/src/__tests__/inline-files.spec.ts similarity index 100% rename from src/__tests__/inline-files.test.ts rename to src/__tests__/inline-files.spec.ts diff --git a/src/__tests__/json.spec.ts b/src/__tests__/json.spec.ts deleted file mode 100644 index e12ba74cde..0000000000 --- a/src/__tests__/json.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { parse, stringify } from '../utils/json'; - -describe('stringify/parse', () => { - test('should (de)serialize undefined', () => { - const val = undefined; - const serialized = stringify(val); - expect(typeof serialized).toBe('string'); - expect(parse(serialized)).toBeUndefined(); - }); - - test('should keep the same order of keys', () => { - const str1 = stringify({ one: 1, two: 2 }); - const str2 = stringify({ two: 2, one: 1 }); - expect(str1).toBe(str2); - }); -}); diff --git a/src/__tests__/ng-jest-config.spec.ts b/src/__tests__/ng-jest-config.spec.ts index ea87ff9eab..b556e70477 100644 --- a/src/__tests__/ng-jest-config.spec.ts +++ b/src/__tests__/ng-jest-config.spec.ts @@ -4,13 +4,15 @@ import { join } from 'path'; import { NgJestConfig } from '../config/ng-jest-config'; describe('NgJestConfig', () => { + const specifiedTsCfgPath = join(__dirname, '..', '..', 'tsconfig.spec.json'); + const defaultTsCfgPath = join(__dirname, '..', '..', 'tsconfig.json'); + describe('readTsConfig', () => { test('should return config including Angular compiler config with tsconfig as a string from ts-jest option', () => { - const configFilePath = join(__dirname, 'tsconfig-ve.json'); const ngJestConfig = new NgJestConfig({ globals: { 'ts-jest': { - tsconfig: configFilePath, + tsconfig: specifiedTsCfgPath, }, }, // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -19,7 +21,7 @@ describe('NgJestConfig', () => { delete config.options.basePath; delete config.options.baseUrl; - expect(config.options.configFilePath as string).toEqual(normalizeSeparators(configFilePath)); + expect(config.options.configFilePath).toEqual(normalizeSeparators(specifiedTsCfgPath)); delete config.options.configFilePath; delete config.options.genDir; expect(config.options).toMatchSnapshot(); @@ -41,9 +43,7 @@ describe('NgJestConfig', () => { delete config.options.basePath; delete config.options.baseUrl; - expect(config.options.configFilePath as string).toEqual( - normalizeSeparators(join(__dirname, '..', '..', 'tsconfig.json')), - ); + expect(config.options.configFilePath).toEqual(normalizeSeparators(defaultTsCfgPath)); delete config.options.configFilePath; delete config.options.genDir; expect(config.options).toMatchSnapshot(); @@ -55,9 +55,7 @@ describe('NgJestConfig', () => { delete config.options.basePath; delete config.options.baseUrl; - expect(config.options.configFilePath as string).toEqual( - normalizeSeparators(join(__dirname, '..', '..', 'tsconfig.json')), - ); + expect(config.options.configFilePath).toEqual(normalizeSeparators(defaultTsCfgPath)); delete config.options.configFilePath; delete config.options.genDir; expect(config.options).toMatchSnapshot(); diff --git a/src/__tests__/reflect-metadata.test.ts b/src/__tests__/reflect-metadata.spec.ts similarity index 100% rename from src/__tests__/reflect-metadata.test.ts rename to src/__tests__/reflect-metadata.spec.ts diff --git a/src/__tests__/strip-styles.test.ts b/src/__tests__/strip-styles.spec.ts similarity index 100% rename from src/__tests__/strip-styles.test.ts rename to src/__tests__/strip-styles.spec.ts diff --git a/src/__tests__/tsconfig-ve.json b/src/__tests__/tsconfig-ve.json deleted file mode 100644 index 22e994dd20..0000000000 --- a/src/__tests__/tsconfig-ve.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "compilerOptions": { - "module": "es2020", - "target": "es2015" - }, - "include": ["../__tests__"], - "angularCompilerOptions": { - "fullTemplateTypeCheck": true, - "strictInjectionParameters": true, - "enableIvy": false - } -} diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 34bb0c1905..cc69d4ff2c 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -3,5 +3,8 @@ "compilerOptions": { "target": "ES5" }, - "include": [] // boost test's speed trick + "include": ["**/__tests__"], + "exclude": [ + "*.spec.ts" + ] }