Skip to content

Commit

Permalink
feat(config): support custom AST transformers written in TypeScript (#…
Browse files Browse the repository at this point in the history
…3063)

Closes #2831
  • Loading branch information
ahnpnl committed Nov 17, 2021
1 parent 3649e73 commit 340a305
Show file tree
Hide file tree
Showing 40 changed files with 611 additions and 132 deletions.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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',
},
}
File renamed without changes.
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"
}
}
}

0 comments on commit 340a305

Please sign in to comment.