Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): support custom AST transformers written in TypeScript #3063

Merged
merged 1 commit into from Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 52 additions & 5 deletions e2e/__tests__/ast-transformers.test.ts
@@ -1,8 +1,14 @@
import path from 'path'

import execa from 'execa'

import { json as runWithJson } from '../run-jest'
import { runNpmInstall } from '../utils'

const { createBundle } = require('../../scripts/lib/bundle')

const AST_TRANSFORMERS_DIR_NAME = 'ast-transformers'

const executeTest = (testDir: string): void => {
test(`successfully runs the tests inside ${testDir} with isolatedModules: false`, () => {
const { json } = runWithJson(testDir)
Expand All @@ -18,15 +24,56 @@ const executeTest = (testDir: string): void => {
}

describe('path-mapping', () => {
executeTest('ast-transformers/path-mapping')
executeTest(`${AST_TRANSFORMERS_DIR_NAME}/path-mapping`)
})

const TRANSFORM_OPT_DIR_NAME = 'transformer-options'

describe('transformer-options', () => {
const TRANSFORM_OPT_DIR_NAME = 'transformer-options'

beforeAll(() => {
runNpmInstall(path.join(__dirname, '..', 'ast-transformers', TRANSFORM_OPT_DIR_NAME))
runNpmInstall(path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, TRANSFORM_OPT_DIR_NAME))
})

test(`successfully runs the tests inside ${AST_TRANSFORMERS_DIR_NAME}/${TRANSFORM_OPT_DIR_NAME}`, () => {
const { json } = runWithJson(`${AST_TRANSFORMERS_DIR_NAME}/${TRANSFORM_OPT_DIR_NAME}`)

expect(json.success).toBe(true)
})
})

describe('hoist-jest', () => {
const NON_TS_FACTORY_DIR_NAME = 'non-ts-factory'
const TS_FACTORY_DIR_NAME = 'ts-factory'

describe('non-ts-factory', () => {
const DIR = path.resolve(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, 'hoist-jest', NON_TS_FACTORY_DIR_NAME)

executeTest(`ast-transformers/${TRANSFORM_OPT_DIR_NAME}`)
beforeAll(() => {
runNpmInstall(DIR)
const bundle = createBundle()
execa.sync('npm', ['install', '--no-package-lock', '--no-shrinkwrap', '--no-save', bundle], {
cwd: DIR,
})
})

executeTest(`${AST_TRANSFORMERS_DIR_NAME}/hoist-jest/${NON_TS_FACTORY_DIR_NAME}`)
})

describe('ts-factory', () => {
beforeAll(() => {
runNpmInstall(path.resolve(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, 'hoist-jest', TS_FACTORY_DIR_NAME))
})

executeTest(`${AST_TRANSFORMERS_DIR_NAME}/hoist-jest/${TS_FACTORY_DIR_NAME}`)
})
})

describe('transformer-in-ts', () => {
const TRANSFORMER_IN_TS_DIR_NAME = `${AST_TRANSFORMERS_DIR_NAME}/transformer-in-ts`

test(`successfully runs the tests inside ${TRANSFORMER_IN_TS_DIR_NAME}`, () => {
const { json } = runWithJson(TRANSFORMER_IN_TS_DIR_NAME)

expect(json.success).toBe(true)
})
})
48 changes: 0 additions & 48 deletions e2e/__tests__/hoist-jest.test.ts

This file was deleted.

File renamed without changes.
File renamed without changes.
Expand Up @@ -14,6 +14,6 @@ module.exports = {
react$: '<rootDir>/node_modules/react',
},
transform: {
'^.+.[tj]sx?$': '<rootDir>/../../../dist/index.js',
'^.+.[tj]sx?$': '<rootDir>/../../../../dist/index.js',
},
}
Expand Up @@ -17,7 +17,7 @@
"react$": "<rootDir>/node_modules/react"
},
"transform": {
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
"^.+\\.[tj]sx?$": "<rootDir>/../../../../dist/index.js"
}
}
}
File renamed without changes.
@@ -0,0 +1,9 @@
import { color } from '../entry'

jest.mock('../entry', () => {
return { color: 'blue' }
})

test('should use custom AST transformer written in ts', () => {
expect(color).toBe('blue')
})
3 changes: 3 additions & 0 deletions e2e/ast-transformers/transformer-in-ts/entry.ts
@@ -0,0 +1,3 @@
type Color = 'red' | 'blue'

export const color: Color = 'red'
43 changes: 43 additions & 0 deletions e2e/ast-transformers/transformer-in-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions e2e/ast-transformers/transformer-in-ts/package.json
@@ -0,0 +1,14 @@
{
"jest": {
"globals": {
"ts-jest": {
"astTransformers": {
"before": ["<rootDir>/../../../src/transformers/hoist-jest.ts"]
}
}
},
"transform": {
"^.+\\.[tj]sx?$": "<rootDir>/../../../dist/index.js"
}
}
}