-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): allow custom options in custom transformers (#1966)
Closes #1942 Co-authored-by: Long Ho <holevietlong@gmail.com>
- Loading branch information
Showing
9 changed files
with
275 additions
and
37 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,18 @@ | ||
const { LogContexts, LogLevels } = require('bs-logger') | ||
|
||
function factory(cs, extraOpts = Object.create(null)) { | ||
const logger = cs.logger.child({ namespace: 'dummy-transformer' }) | ||
const ts = cs.compilerModule | ||
logger.debug('Dummy transformer with extra options', JSON.stringify(extraOpts)) | ||
|
||
function createVisitor(_ctx, _sf) { | ||
return (node) => node | ||
} | ||
|
||
return (ctx) => | ||
logger.wrap({ [LogContexts.logLevel]: LogLevels.debug, call: null }, 'visitSourceFileNode(): dummy', (sf) => | ||
ts.visitNode(sf, createVisitor(ctx, sf)) | ||
) | ||
} | ||
|
||
exports.factory = factory |
5 changes: 5 additions & 0 deletions
5
e2e/__cases__/ast-transformers/with-extra-options/with-extra-options.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,5 @@ | ||
const a = 1; | ||
|
||
it('should pass', () => { | ||
expect(a).toEqual(1); | ||
}) |
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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`AST transformers with extra options should pass using template "default" 1`] = ` | ||
Array [ | ||
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}", | ||
] | ||
`; | ||
|
||
exports[`AST transformers with extra options should pass using template "with-babel-7" 1`] = ` | ||
Array [ | ||
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}", | ||
] | ||
`; | ||
|
||
exports[`AST transformers with extra options should pass using template "with-babel-7-string-config" 1`] = ` | ||
Array [ | ||
"[level:20] Dummy transformer with extra options {\\"foo\\":\\"bar\\"}", | ||
] | ||
`; |
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,38 @@ | ||
import { configureTestCase } from '../__helpers__/test-case' | ||
import { allValidPackageSets } from '../__helpers__/templates' | ||
import { existsSync } from "fs" | ||
import { LogContexts, LogLevels } from 'bs-logger' | ||
|
||
describe('AST transformers', () => { | ||
describe('with extra options', () => { | ||
const testCase = configureTestCase('ast-transformers/with-extra-options', { | ||
env: { TS_JEST_LOG: 'ts-jest.log' }, | ||
tsJestConfig: { | ||
astTransformers: { | ||
before: [{ | ||
path: require.resolve('../__cases__/ast-transformers/with-extra-options/foo'), | ||
options: { | ||
foo: 'bar', | ||
}, | ||
}], | ||
}, | ||
}, | ||
}) | ||
|
||
testCase.runWithTemplates(allValidPackageSets, 0, (runTest, { testLabel }) => { | ||
it(testLabel, () => { | ||
const result = runTest() | ||
expect(result.status).toBe(0) | ||
expect(existsSync(result.logFilePath)).toBe(true) | ||
const filteredEntries = result.logFileEntries | ||
// keep only debug and above | ||
.filter(m => (m.context[LogContexts.logLevel] || 0) >= LogLevels.debug) | ||
// simplify entries | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
.map(e => result.normalize(`[level:${e.context[LogContexts.logLevel]}] ${e.message}`)) | ||
.filter(logging => logging.includes('Dummy transformer with extra options')) | ||
expect(filteredEntries).toMatchSnapshot() | ||
}) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.