-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: implement more real-world testing ability
- Loading branch information
Showing
21 changed files
with
303 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "ts-jest-e2e-tmpl", | ||
"version": "0.0.0-test.0", | ||
"private": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Hello { | ||
constructor(readonly msg: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
declare var jest, describe, it, expect; | ||
|
||
import { Hello } from '../Hello'; | ||
|
||
describe('Hello Class', () => { | ||
it('should create a new Hello', () => { | ||
const hello = new Hello('foo'); | ||
expect(hello.msg).toBe('foo'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"jest": { | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"moduleFileExtensions": ["ts", "js"], | ||
"testEnvironment": "node" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { runE2eTest } from '../../tests/__helpers__/jest-runner'; | ||
|
||
describe('Simple e2e test', () => { | ||
it('should run the tests with success', () => { | ||
expect('simple').toBeE2eTestWithExitCode(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require('./jest.config'); | ||
|
||
module.exports = Object.assign({}, base, { | ||
testRegex: 'e2e/__tests__/.*(?!watch)\\.spec\\.ts$', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.tsx?$': '<rootDir>/dist/index.js', | ||
}, | ||
testRegex: '(tests|e2e)/__tests__/.+\\.spec\\.ts$', | ||
testPathIgnorePatterns: ['/node_modules/', '/watch.spec.ts$'], | ||
coverageReporters: ['text'], | ||
coverageDirectory: 'test_coverage_dir', | ||
collectCoverageFrom: ['src/**/*.tsx', 'src/**/*.ts'], | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], | ||
globals: { | ||
'ts-jest': { | ||
tsConfigFile: 'tsconfig.base.json', | ||
}, | ||
}, | ||
setupTestFrameworkScriptFile: | ||
'<rootDir>/tests/__helpers__/setup-test-framework.ts', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require('./jest.config'); | ||
|
||
module.exports = Object.assign({}, base, { | ||
testRegex: 'tests/__tests__/.*(?!watch)\\.spec\\.ts$', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env node | ||
|
||
const rimraf = require('rimraf'); | ||
const Paths = require('./paths'); | ||
const { join } = require('path'); | ||
|
||
rimraf.sync(Paths.distDir); | ||
rimraf.sync(join(Paths.testsRootDir, '*', 'coverage')); | ||
rimraf.sync(join(Paths.testsRootDir, '*', 'debug.txt')); | ||
rimraf.sync(join(Paths.testsRootDir, '*', 'node_modules')); | ||
rimraf.sync(Paths.e2eWorkDir); | ||
rimraf.sync(Paths.e2eWotkDirLink); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
process.env.NODE_ENV = 'test'; | ||
const jest = require('jest'); | ||
const { sync: spawnSync } = require('cross-spawn'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const Paths = require('./paths'); | ||
|
||
function getDirectories(rootDir) { | ||
return fs.readdirSync(rootDir).filter(function(file) { | ||
return fs.statSync(path.join(rootDir, file)).isDirectory(); | ||
}); | ||
} | ||
|
||
function setupE2e() { | ||
// this will trigger the build as well (not using yarn since yarn pack is bugy) | ||
// keep on to so that the build is triggered beforehand (pack => prepublish => clean-build => build) | ||
const res = spawnSync('npm', ['-s', 'pack'], { cwd: Paths.rootDir }); | ||
const bundle = path.join(Paths.rootDir, res.stdout.toString().trim()); | ||
|
||
// ensure directory exists before copying over | ||
fs.mkdirpSync(Paths.e2eWorkTemplateDir); | ||
|
||
// create the tempalte package from which node_modules will be originally copied from | ||
fs.copyFileSync( | ||
path.join(Paths.e2eSourceDir, 'package-template.json'), | ||
path.join(Paths.e2eWorkTemplateDir, 'package.json') | ||
); | ||
// link locally so we could find it easily | ||
fs.symlinkSync(Paths.e2eWorkDir, Paths.e2eWotkDirLink); | ||
// TODO: run with specific versions? | ||
spawnSync('npm', ['i', '-D', 'jest', 'typescript', bundle], { | ||
cwd: Paths.e2eWorkTemplateDir, | ||
stdio: 'inherit', | ||
}); | ||
|
||
// copy into our temp sub-folder | ||
getDirectories(Paths.e2eSourceDir).forEach(directory => { | ||
// copy source and test files | ||
fs.copySync( | ||
path.join(Paths.e2eSourceDir, directory), | ||
path.join(Paths.e2eWorkDir, directory) | ||
); | ||
// create the node_modules dir | ||
const caseNodeModulesDir = path.join( | ||
Paths.e2eWorkDir, | ||
directory, | ||
'node_modules' | ||
); | ||
fs.mkdirpSync(caseNodeModulesDir); | ||
// link each node_modules from tempalte dir (so that we can install some more specific in each package later) | ||
const tmplNodeModulesDir = path.join( | ||
Paths.e2eWorkTemplateDir, | ||
'node_modules' | ||
); | ||
getDirectories(tmplNodeModulesDir).forEach(moduleDir => { | ||
// avoid linking '.bin' | ||
if (moduleDir === '.bin') return; | ||
fs.symlinkSync( | ||
path.join(tmplNodeModulesDir, moduleDir), | ||
path.join(caseNodeModulesDir, moduleDir) | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
setupE2e(); | ||
|
||
const argv = process.argv.slice(2); | ||
argv.push('--no-cache'); | ||
argv.push('--config', path.join(Paths.rootDir, 'jest.config.e2e.js')); | ||
jest.run(argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const path = require('path'); | ||
const os = require('os'); | ||
|
||
const rootDir = path.resolve(__dirname, '..'); | ||
const distDir = path.join(rootDir, 'dist'); | ||
const testsRootDir = path.join(rootDir, 'tests'); | ||
const e2eRootDir = path.join(rootDir, 'e2e'); | ||
const e2eSourceDir = path.join(e2eRootDir, '__cases__'); | ||
const e2eTestsDir = path.join(e2eRootDir, '__tests__'); | ||
const e2eWorkDir = path.join(os.tmpdir(), '--ts-jest-temp-e2e--'); | ||
const e2eWorkTemplateDir = path.join(e2eWorkDir, '__template__'); | ||
const e2eWotkDirLink = path.join(e2eRootDir, '__e2e_workdir_link__'); | ||
|
||
module.exports = { | ||
rootDir, | ||
e2eSourceDir, | ||
e2eRootDir, | ||
e2eWorkDir, | ||
e2eWorkTemplateDir, | ||
e2eWotkDirLink, | ||
distDir, | ||
testsRootDir, | ||
e2eTestsDir, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'jest'; | ||
|
||
declare namespace jest { | ||
interface Matchers<R> { | ||
toBeE2eTestWithExitCode(expectedExitCode: number): R; | ||
toBeTestCaseWithExitCode(expectedExitCode: number): R; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// from: https://github.com/facebook/jest/blob/master/integration_tests/runJest.js | ||
|
||
import { sync as spawnSync } from 'cross-spawn'; | ||
import * as path from 'path'; | ||
import * as Paths from '../../scripts/paths'; | ||
|
||
function runJestTestIn( | ||
dir: string, | ||
args: string[] = [], | ||
env = {}, | ||
): JestRunResult { | ||
if (!path.isAbsolute(dir)) { | ||
throw new Error(`You must give an absolute path to runJestTestIn().`); | ||
} | ||
|
||
const JEST_BIN = path.join(dir, 'node_modules', 'jest', 'bin', 'jest.js'); | ||
|
||
const result = spawnSync(JEST_BIN, args, { | ||
cwd: dir, | ||
// Add both process.env which is the standard and custom env variables | ||
env: { ...process.env, ...env }, | ||
}); | ||
|
||
// Call to string on byte arrays and strip ansi color codes for more accurate string comparison. | ||
const stdout = result.stdout ? stripAnsiColors(result.stdout.toString()) : ''; | ||
const stderr = result.stderr ? stripAnsiColors(result.stderr.toString()) : ''; | ||
const output = result.output | ||
? stripAnsiColors(result.output.join('\n\n')) | ||
: ''; | ||
|
||
return { status: result.status, stderr, stdout, output }; | ||
} | ||
|
||
// from https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings | ||
function stripAnsiColors(stringToStrip: string): string { | ||
return stringToStrip.replace( | ||
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, | ||
'', | ||
); | ||
} | ||
|
||
export interface JestRunResult { | ||
status: number; | ||
stdout: string; | ||
stderr: string; | ||
output: string; | ||
} | ||
|
||
export function runTestCase( | ||
name: string, | ||
{ env = {}, args = [] } = {}, | ||
): JestRunResult { | ||
return runJestTestIn(path.join(Paths.testsRootDir, name), args, env); | ||
} | ||
|
||
export function runE2eTest( | ||
name: string, | ||
{ env = {}, args = [] } = {}, | ||
): JestRunResult { | ||
return runJestTestIn(path.join(Paths.e2eWorkDir, name), args, env); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { runE2eTest, runTestCase } from './jest-runner'; | ||
|
||
function expectTest(runner: any, received, expectedStatus) { | ||
const { status, output } = runner(received); | ||
const pass: boolean = expectedStatus === status; | ||
if (pass) { | ||
return { | ||
message: () => | ||
`expected e2e test ${received} to not exit with status ${expectedStatus}` + | ||
`\n\nOutput:\n ${this.utils.printReceived(output)}`, | ||
pass: true, | ||
}; | ||
} else { | ||
return { | ||
message: () => | ||
`expected e2e test ${received} to exit with status ${expectedStatus}` + | ||
`\n\nOutput:\n ${this.utils.printReceived(output)}`, | ||
pass: false, | ||
}; | ||
} | ||
} | ||
|
||
expect.extend({ | ||
toBeE2eTestWithExitCode(received: string, expectedStatus: number) { | ||
return expectTest.call(this, runE2eTest, received, expectedStatus); | ||
}, | ||
toBeTestCaseWithExitCode(received: string, expectedStatus: number) { | ||
return expectTest.call(this, runTestCase, received, expectedStatus); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters