Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): add styled-jsx to babel jest setup to avoid warnings
- Loading branch information
1 parent
7218f81
commit 1869e1b
Showing
11 changed files
with
144 additions
and
28 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
29 changes: 19 additions & 10 deletions
29
packages/next/src/schematics/application/lib/update-jest-config.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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
import { noop, Rule } from '@angular-devkit/schematics'; | ||
import { chain, noop, Rule } from '@angular-devkit/schematics'; | ||
import { NormalizedSchema } from './normalize-options'; | ||
import { updateBabelJestConfig } from '@nrwl/react/src/rules/update-babel-jest-config'; | ||
|
||
export function updateJestConfig(options: NormalizedSchema): Rule { | ||
return options.unitTestRunner === 'none' | ||
? noop() | ||
: (host) => { | ||
const configPath = `${options.appProjectRoot}/jest.config.js`; | ||
const originalContent = host.read(configPath).toString(); | ||
const content = originalContent.replace( | ||
'transform: {', | ||
"transform: {\n '^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest'," | ||
); | ||
host.overwrite(configPath, content); | ||
}; | ||
: chain([ | ||
(host) => { | ||
const configPath = `${options.appProjectRoot}/jest.config.js`; | ||
const originalContent = host.read(configPath).toString(); | ||
const content = originalContent.replace( | ||
'transform: {', | ||
"transform: {\n '^(?!.*\\\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest'," | ||
); | ||
host.overwrite(configPath, content); | ||
}, | ||
updateBabelJestConfig(options.appProjectRoot, (json) => { | ||
if (options.style === 'styled-jsx') { | ||
json.plugins = (json.plugins || []).concat('styled-jsx/babel'); | ||
} | ||
return json; | ||
}), | ||
]); | ||
} |
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,41 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { createEmptyWorkspace } from '@nrwl/workspace/testing'; | ||
import { readJsonInTree } from '@nrwl/workspace'; | ||
import { updateBabelJestConfig } from './update-babel-jest-config'; | ||
import { callRule } from '@nrwl/workspace/src/utils/testing'; | ||
|
||
describe('updateBabelJestConfig', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(() => { | ||
tree = Tree.empty(); | ||
tree = createEmptyWorkspace(tree); | ||
}); | ||
|
||
it('should update babel-jest.config.json', async () => { | ||
tree.create('/apps/demo/babel-jest.config.json', JSON.stringify({})); | ||
|
||
tree = await callRule( | ||
updateBabelJestConfig('/apps/demo', (json) => { | ||
json.plugins = ['test']; | ||
return json; | ||
}), | ||
tree | ||
); | ||
|
||
const config = readJsonInTree(tree, '/apps/demo/babel-jest.config.json'); | ||
expect(config.plugins).toEqual(['test']); | ||
}); | ||
|
||
it('should do nothing if project does not use babel jest', async () => { | ||
tree = await callRule( | ||
updateBabelJestConfig('/apps/demo', (json) => { | ||
json.plugins = ['test']; | ||
return json; | ||
}), | ||
tree | ||
); | ||
|
||
expect(tree.exists('/apps/demo/babel-jest.config.json')).toBe(false); | ||
}); | ||
}); |
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,16 @@ | ||
import { noop, Tree } from '@angular-devkit/schematics'; | ||
import { updateJsonInTree } from '@nrwl/workspace'; | ||
|
||
type BabelJestConfigUpdater<T> = (json: T) => T; | ||
|
||
export function updateBabelJestConfig<T = any>( | ||
projectRoot: string, | ||
update: BabelJestConfigUpdater<T> | ||
) { | ||
return (host: Tree) => { | ||
const configPath = `${projectRoot}/babel-jest.config.json`; | ||
return host.exists(configPath) | ||
? updateJsonInTree<T>(configPath, update) | ||
: noop(); | ||
}; | ||
} |
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
25 changes: 17 additions & 8 deletions
25
packages/react/src/schematics/application/lib/update-jest-config.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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import { noop, Rule } from '@angular-devkit/schematics'; | ||
import { updateJestConfigContent } from '@nrwl/react/src/utils/jest-utils'; | ||
import { chain, noop, Rule } from '@angular-devkit/schematics'; | ||
import { updateBabelJestConfig } from '../../../rules/update-babel-jest-config'; | ||
import { updateJestConfigContent } from '../../../utils/jest-utils'; | ||
import { NormalizedSchema } from '../schema'; | ||
|
||
export function updateJestConfig(options: NormalizedSchema): Rule { | ||
return options.unitTestRunner === 'none' | ||
? noop() | ||
: (host) => { | ||
const configPath = `${options.appProjectRoot}/jest.config.js`; | ||
const originalContent = host.read(configPath).toString(); | ||
const content = updateJestConfigContent(originalContent); | ||
host.overwrite(configPath, content); | ||
}; | ||
: chain([ | ||
(host) => { | ||
const configPath = `${options.appProjectRoot}/jest.config.js`; | ||
const originalContent = host.read(configPath).toString(); | ||
const content = updateJestConfigContent(originalContent); | ||
host.overwrite(configPath, content); | ||
}, | ||
updateBabelJestConfig(options.appProjectRoot, (json) => { | ||
if (options.style === 'styled-jsx') { | ||
json.plugins = (json.plugins || []).concat('styled-jsx/babel'); | ||
} | ||
return json; | ||
}), | ||
]); | ||
} |
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