Skip to content

Commit

Permalink
test(unit): add mock data, rename tests (#610)
Browse files Browse the repository at this point in the history
Separate from #562
  • Loading branch information
ahnpnl authored Nov 5, 2020
1 parent 74b3e0f commit 29074dd
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 42 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = {
tsconfig: 'tsconfig.spec.json',
},
},
testPathIgnorePatterns: ['/e2e/'],
testPathIgnorePatterns: ['/e2e/', '/src/__tests__/__mocks__/'],
};
10 changes: 10 additions & 0 deletions src/__tests__/__mocks__/app.component.ts
Original file line number Diff line number Diff line change
@@ -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';
}
9 changes: 9 additions & 0 deletions src/__tests__/__mocks__/foo.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from '@angular/core';

@Component({
selector: 'foo-root',
template: `<h1>Hello World !!</h1>`,
})
export class FooComponent {
title: number = 'test-app-v10';
}
9 changes: 9 additions & 0 deletions src/__tests__/__mocks__/foo.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Injectable } from '@angular/core';

export class ClassInject {}

@Injectable()
export class MyService {
// eslint-disable-next-line
constructor(_v: ClassInject) {}
}
5 changes: 5 additions & 0 deletions src/__tests__/__mocks__/foo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { getFoo } from './foo';

jest.mock('./foo');

console.log(getFoo());
3 changes: 3 additions & 0 deletions src/__tests__/__mocks__/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function getFoo(): string {
return 'foo';
}
23 changes: 20 additions & 3 deletions src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
}
`;

Expand Down
File renamed without changes.
16 changes: 0 additions & 16 deletions src/__tests__/json.spec.ts

This file was deleted.

16 changes: 7 additions & 9 deletions src/__tests__/ng-jest-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 0 additions & 12 deletions src/__tests__/tsconfig-ve.json

This file was deleted.

5 changes: 4 additions & 1 deletion tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"compilerOptions": {
"target": "ES5"
},
"include": [] // boost test's speed trick
"include": ["**/__tests__"],
"exclude": [
"*.spec.ts"
]
}

0 comments on commit 29074dd

Please sign in to comment.