Skip to content

Commit 0edae8b

Browse files
committed
test: reorganize tests and fixtures
1 parent a9644f1 commit 0edae8b

33 files changed

+263
-190
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { pseudoRandomBytes } from 'crypto'
2+
3+
const bar = pseudoRandomBytes(64).toString('utf8')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { pseudoRandomBytes } from 'crypto'
2+
3+
const foo = pseudoRandomBytes(32).toString('utf8')
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { hello } from './second'
2+
3+
export const variable = 1
4+
5+
export const greet = () => {
6+
console.log(hello)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const hello = 'Hello World'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2017",
4+
"module": "commonjs",
5+
"lib": ["es2017"],
6+
"rootDir": "./src",
7+
"outDir": "./dist",
8+
"strict": true,
9+
"moduleResolution": "node",
10+
"esModuleInterop": true,
11+
"resolveJsonModule": true
12+
}
13+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { pseudoRandomBytes } from 'crypto'
2+
3+
const bar = pseudoRandomBytes(64).toString('utf8')
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { pseudoRandomBytes } from 'crypto'
2+
3+
const foo = pseudoRandomBytes(32).toString('utf8')

__fixtures__/basic/src/main.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { hello } from './second'
2+
3+
export const variable = 1
4+
5+
export const greet = () => {
6+
console.log(hello)
7+
}

__fixtures__/basic/src/second.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const hello = 'Hello World'

__fixtures__/basic/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2017",
4+
"module": "commonjs",
5+
"lib": ["es2017"],
6+
"rootDir": "./src",
7+
"outDir": "./dist",
8+
"strict": true,
9+
"moduleResolution": "node",
10+
"esModuleInterop": true,
11+
"resolveJsonModule": true
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"jsonFixture": null,
3+
"default": 1
4+
}
File renamed without changes.

__fixtures__/src/module/functions-variables.ts renamed to __fixtures__/bundle-complex/src/module/functions-variables.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type KV = {
3636
* @internal
3737
*/
3838
export const isObservable: (obj: KV) => boolean = (obj) => {
39-
return !!obj && (isFunction(obj.lift) && isFunction(obj.subscribe))
39+
return !!obj && isFunction(obj.lift) && isFunction(obj.subscribe)
4040
}
4141

4242
export const isObs = isObservable
@@ -91,8 +91,8 @@ export function OverloadedDecorator(): ParameterDecorator {
9191
return (target, methodKey, index) => undefined
9292
}
9393

94-
export { jsonFixture as fixture } from '../other/data.json'
95-
// import { jsonFixture as fixture } from '../other/data.json'
94+
export { jsonFixture as fixture } from '../data.json'
95+
// import { jsonFixture as fixture } from '../data.json'
9696
// export { fixture }
97-
import * as jsonFile from '../other/data.json'
97+
import * as jsonFile from '../data.json'
9898
export { jsonFile }

__fixtures__/src/excluded/foo.file.ts

-3
This file was deleted.

__tests__/builder.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { join } from 'path'
2+
import { existsSync } from 'fs'
3+
import { createProgramFromConfig, build } from '../src'
4+
import { rmrf } from '../src/utils/fs'
5+
6+
const basePath = join(__dirname, '..', '__fixtures__', 'basic')
7+
8+
afterEach(() => {
9+
rmrf(join(basePath, 'dist'))
10+
})
11+
12+
test('Create program by overriding config file', async () => {
13+
const consoleWarnSpy = spyOn(console, 'warn')
14+
15+
const program = createProgramFromConfig({
16+
basePath,
17+
configFilePath: 'tsconfig.json',
18+
compilerOptions: {
19+
rootDir: 'src',
20+
outDir: 'dist',
21+
declaration: 'true' as any,
22+
skipLibCheck: true,
23+
},
24+
exclude: ['**/excluded', '**/dist'],
25+
})
26+
27+
expect(program.getCompilerOptions()).toMatchObject({
28+
strict: true,
29+
// `compilerOptions` properties returns unix separators in windows paths
30+
rootDir: join(basePath, 'src').replace(/\\/g, '/'),
31+
declaration: undefined,
32+
})
33+
34+
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining("'declaration' requires a value of type boolean"))
35+
36+
expect(program.getRootFileNames()).toHaveLength(2)
37+
})
38+
39+
test('Build without errors with config from scratch', async () => {
40+
const consoleWarnSpy = spyOn(console, 'warn')
41+
42+
build({
43+
basePath,
44+
compilerOptions: {
45+
module: 'commonjs',
46+
moduleResolution: 'node',
47+
target: 'es2019',
48+
lib: ['es2019'],
49+
rootDir: 'src',
50+
outDir: 'dist',
51+
resolveJsonModule: true,
52+
// listEmittedFiles: true,
53+
esModuleInterop: true,
54+
listFiles: true,
55+
declaration: true,
56+
skipLibCheck: true,
57+
},
58+
})
59+
60+
expect(consoleWarnSpy).not.toHaveBeenCalledWith(expect.stringContaining('error'))
61+
62+
const distMainFile = join(basePath, 'dist', 'main.js')
63+
expect(existsSync(distMainFile)).toBe(true)
64+
})

__tests__/bundle-addon.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { join } from 'path'
2+
import { build } from '../src'
3+
4+
const basePath = join(__dirname, '..', '__fixtures__', 'bundle-complex')
5+
6+
test.skip('bundle', () => {
7+
build({
8+
basePath,
9+
configFilePath: 'tsconfig.json',
10+
compilerOptions: {
11+
declaration: true,
12+
listEmittedFiles: true,
13+
},
14+
exclude: ['**/excluded'],
15+
bundleDeclaration: {
16+
entryPoint: 'main.d.ts',
17+
},
18+
})
19+
})

__tests__/clean-addon.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { join } from 'path'
2+
import { build } from '../src'
3+
4+
const basePath = join(__dirname, '..', '__fixtures__', 'basic')
5+
6+
// Mock deleting folders for protection
7+
jest.mock('../src/utils/fs', () => ({
8+
...jest.requireActual('../src/utils/fs'),
9+
rmrf: jest.fn((path) => console.info('mock rmrf on', path)),
10+
}))
11+
12+
// Need actual delete implementation to clean between some tests.
13+
// Can't unmock implicit imports on a per test basis https://github.com/facebook/jest/issues/2649
14+
afterEach(() => {
15+
const { rmrf } = jest.requireActual('../src/utils/fs')
16+
const outDirPaths = [join(basePath, 'dist'), join(basePath, 'src', 'dist')]
17+
outDirPaths.forEach(rmrf)
18+
})
19+
20+
describe('Clean protections', () => {
21+
test('Forbid cleaning rootDir', async () => {
22+
expect(() => {
23+
build({
24+
basePath,
25+
configFilePath: 'tsconfig.json',
26+
compilerOptions: { rootDir: 'src' },
27+
clean: ['src'],
28+
})
29+
}).toThrow('cannot delete')
30+
})
31+
32+
test('Forbid cleaning basePath and up', async () => {
33+
expect(() => {
34+
build({
35+
basePath,
36+
configFilePath: 'tsconfig.json',
37+
clean: ['.'],
38+
})
39+
}).toThrow('cannot delete')
40+
41+
expect(() => {
42+
build({
43+
basePath,
44+
configFilePath: 'tsconfig.json',
45+
clean: ['..'],
46+
})
47+
}).toThrow('cannot delete')
48+
})
49+
50+
test('Forbid cleaning cwd', async () => {
51+
expect(() => {
52+
build({
53+
basePath,
54+
configFilePath: 'tsconfig.json',
55+
clean: [process.cwd()],
56+
})
57+
}).toThrow('cannot delete')
58+
})
59+
})

__tests__/copy-addon.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { existsSync, readdirSync } from 'fs'
2+
import { join } from 'path'
3+
import { build } from '../src'
4+
import { rmrf } from '../src/utils/fs'
5+
6+
const basePath = join(__dirname, '..', '__fixtures__', 'basic-with-other')
7+
8+
afterEach(() => {
9+
rmrf(join(basePath, 'dist'))
10+
rmrf(join(basePath, 'src', 'dist'))
11+
})
12+
13+
describe('Copy addon', () => {
14+
test('all other files', () => {
15+
const expectedOtherFiles = readdirSync(join(basePath, 'src', 'other'))
16+
const consoleInfoSpy = spyOn(console, 'info')
17+
18+
build({
19+
basePath,
20+
configFilePath: 'tsconfig.json',
21+
copyOtherToOutDir: true,
22+
exclude: ['**/excluded'],
23+
})
24+
25+
const otherDirDistPath = join(basePath, 'dist', 'other')
26+
expect(readdirSync(otherDirDistPath)).toHaveLength(expectedOtherFiles.length)
27+
28+
const excludedDirDistPath = join(basePath, 'dist', 'excluded')
29+
expect(existsSync(excludedDirDistPath)).toBe(false)
30+
31+
expect(consoleInfoSpy).toHaveBeenCalledWith(expect.stringMatching(/override.*main\.js/))
32+
})
33+
34+
test('do not recursively copy outDir to outDir', () => {
35+
// tslint:disable-next-line: no-shadowed-variable
36+
const basePath = join(__dirname, '..', '__fixtures__', 'basic-with-other', 'src')
37+
38+
build({
39+
basePath,
40+
configFilePath: '../tsconfig.json',
41+
copyOtherToOutDir: true,
42+
compilerOptions: {
43+
rootDir: '.',
44+
outDir: 'dist',
45+
},
46+
exclude: ['**/excluded'],
47+
})
48+
49+
const distInDist = join(basePath, 'dist', 'dist')
50+
expect(existsSync(distInDist)).toBe(false)
51+
})
52+
})

jest.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
const config = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
5-
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/'],
5+
roots: ['<rootDir>/src/', '<rootDir>/__tests__/'],
6+
testPathIgnorePatterns: ['/node_modules/', '/__fixtures__/', '.*\\.d\\.ts$'],
67
globals: {
78
'ts-jest': {
89
diagnostics: {
9-
warnOnly: true, // https://kulshekhar.github.io/ts-jest/user/config/diagnostics
10+
// https://kulshekhar.github.io/ts-jest/user/config/diagnostics
11+
warnOnly: true,
12+
pretty: false,
1013
},
1114
},
1215
},

0 commit comments

Comments
 (0)