-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify jest config test helper + moves test utils
- removes complex jest-config test helpers and simplify the tool - removes unused (anymore) rootDir() related code - improves readability of html-transform test - moves `test-utils.ts` into `__helpers__` dir
- Loading branch information
Showing
11 changed files
with
116 additions
and
278 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
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 was deleted.
Oops, something went wrong.
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,26 @@ | ||
import { TsJestConfig } from '../../dist/types'; | ||
|
||
const { resolve } = require.requireActual('path'); | ||
|
||
/** | ||
* Mock a jest config object for the test case in given folder | ||
* | ||
* Basically it defines `rootDir` and `cwd` properties to the full path of that | ||
* test case, as Jest would do. | ||
* | ||
* Accepts an optional config object, which will be defined on `globals.ts-jest` | ||
*/ | ||
export default function mockJestConfig( | ||
testCaseFolder: string, | ||
tsJest: TsJestConfig | null = null, | ||
): jest.ProjectConfig { | ||
// resolves the path since jest would give a resolved path | ||
const rootDir = resolve(__dirname, '..', testCaseFolder); | ||
// create base jest config object | ||
let options: any = { rootDir, cwd: rootDir }; | ||
// adds TS Jest options if any given | ||
if (tsJest != null) { | ||
options.globals = { 'ts-jest': tsJest }; | ||
} | ||
return options; | ||
} |
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 |
---|---|---|
@@ -1,32 +1,47 @@ | ||
import * as tsJest from '../../dist'; | ||
import jestConfig from '../__helpers__/jest-config'; | ||
import mockJestConfig from '../__helpers__/mock-jest-config'; | ||
|
||
const config = jestConfig.simple({}); | ||
const filePath = `${config.rootDir}/some-file.html`; | ||
const TEST_CASE = 'simple'; | ||
const FILENAME = 'some-file.html'; | ||
|
||
// wrap a transformed source so that we can fake a `require()` on it by calling the returned wrapper | ||
const wrap = (src: string) => | ||
new Function(`var module={}; ${src} return module.exports;`) as any; | ||
|
||
const source = `<div class="html-test"> | ||
const fileContent = `<div class="html-test"> | ||
<span class="html-test__element">This is element</span> | ||
<code>This is a backtilt \`</code> | ||
</div>`; | ||
|
||
describe('Html transforms', () => { | ||
it('transforms html if config.globals.__TRANSFORM_HTML__ is set', () => { | ||
let jestConfig; | ||
|
||
// get the untransformed version | ||
const untransformed = tsJest.process(source, filePath, { ...config }); | ||
// ... then the one which should be transformed | ||
(config.globals as any).__TRANSFORM_HTML__ = true; | ||
const transformed = tsJest.process(source, filePath, config) as string; | ||
// ... finally the result of a `require('module-with-transformed-version')` | ||
const exported = wrap(transformed)(); | ||
jestConfig = mockJestConfig(TEST_CASE); | ||
const untransformed = tsJest.process( | ||
fileContent, | ||
`${jestConfig.rootDir}/${FILENAME}`, | ||
jestConfig, | ||
); | ||
expect(untransformed).toBe(fileContent); | ||
expect(untransformed).toMatchSnapshot('untransformed'); | ||
|
||
expect(exported).toMatchSnapshot('module'); | ||
// ... then the one which should be transformed | ||
jestConfig = { | ||
...mockJestConfig(TEST_CASE), | ||
globals: { __TRANSFORM_HTML__: true }, | ||
}; | ||
const transformed = tsJest.process( | ||
fileContent, | ||
`${jestConfig.rootDir}/${FILENAME}`, | ||
jestConfig, | ||
) as string; | ||
expect(transformed).not.toBe(fileContent); | ||
expect(transformed).toMatchSnapshot('source'); | ||
expect(untransformed).toMatchSnapshot('untransformed'); | ||
// requiring the transformed version should return the same string as the untransformed version | ||
expect(exported).toBe(untransformed); | ||
|
||
// ... finally the result of a `require('module-with-transformed-version')` | ||
const value = eval( | ||
`(function(){const module={}; ${transformed}; return module.exports;})()`, | ||
); | ||
expect(value).toMatchSnapshot('module'); | ||
// the value should be the same string as the source version | ||
expect(value).toBe(fileContent); | ||
}); | ||
}); |
Oops, something went wrong.