Skip to content

Commit 205f1ad

Browse files
author
Justin Helmer
committed
feat: update to support babel config bool/string/object
1 parent b40094f commit 205f1ad

File tree

3 files changed

+196
-11
lines changed

3 files changed

+196
-11
lines changed

README.md

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
5757
- **typescript** (`lang="ts"`, `lang="typescript"`)
5858
- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)
5959

60-
To define a tsconfig file that vue-jest will use when transpiling typescript, you can specify it in the jest globals
60+
### Global Jest options
61+
62+
You can change the behavior of `vue-jest` by using `jest.globals`.
63+
64+
> *Tip:* Need programmatic configuration? Use the [--config](https://jestjs.io/docs/en/cli.html#config-path) option in Jest CLI, and export a `.js` file
65+
66+
#### tsConfigFile
67+
68+
Provide a path to a `tsconfig` file that `vue-jest` will use when transpiling typescript:
6169

6270
```json
6371
{
@@ -71,7 +79,9 @@ To define a tsconfig file that vue-jest will use when transpiling typescript, yo
7179
}
7280
```
7381

74-
To define a babelrc file that vue-jest will use when transpiling javascript, you can specify it in the jest globals
82+
#### babelRcFile
83+
84+
Provide a path to a `.babelrc` file that `vue-jest` will use when transpiling javascript:
7585

7686
```json
7787
{
@@ -85,6 +95,105 @@ To define a babelrc file that vue-jest will use when transpiling javascript, you
8595
}
8696
```
8797

98+
#### babelConfig
99+
100+
Provide `babelConfig` in one of the following formats:
101+
102+
- `<Boolean>`
103+
- `<Object>`
104+
- `<String>`
105+
106+
##### Boolean
107+
108+
- `true` - Enable Babel processing. `vue-jest` will try to find babel configuration using [find-babel-config](https://www.npmjs.com/package/find-babel-config).
109+
110+
> This is the default behavior if [babelRcFile](#babelrcfile) and [babelConfig](#babelconfig) are both undefined.
111+
112+
- `false` - Skip Babel processing entirely:
113+
114+
```json
115+
{
116+
"jest": {
117+
"globals": {
118+
"vue-jest": {
119+
"babelConfig": false
120+
}
121+
}
122+
}
123+
}
124+
```
125+
126+
##### Object
127+
128+
Provide inline [Babel options](https://babeljs.io/docs/en/options):
129+
130+
```json
131+
{
132+
"jest": {
133+
"globals": {
134+
"vue-jest": {
135+
"babelConfig": {
136+
"presets": [
137+
[
138+
"env",
139+
{
140+
"useBuiltIns": "entry",
141+
"shippedProposals": true
142+
}
143+
]
144+
],
145+
"plugins": [
146+
"syntax-dynamic-import"
147+
],
148+
"env": {
149+
"test": {
150+
"plugins": [
151+
"dynamic-import-node"
152+
]
153+
}
154+
}
155+
}
156+
}
157+
}
158+
}
159+
}
160+
```
161+
162+
##### String
163+
164+
If a string is provided, it will be an assumed path to a babel configuration file.
165+
- This is similar to the [babelRcFile](#babelrcfile) option, except it also allows for `.js` files (i.e. `.babelrc.js`).
166+
- Config file should export a Babel configuration object.
167+
- Should *not* point to a [project-wide configuration file (babel.config.js)](https://babeljs.io/docs/en/config-files#project-wide-configuration), which exports a function.
168+
169+
```json
170+
{
171+
"jest": {
172+
"globals": {
173+
"vue-jest": {
174+
"babelConfig": "path/to/.babelrc.js"
175+
}
176+
}
177+
}
178+
}
179+
```
180+
181+
To use the [Config Function API](https://babeljs.io/docs/en/config-files#config-function-api), use inline options instead. i.e.:
182+
183+
```json
184+
{
185+
"jest": {
186+
"globals": {
187+
"vue-jest": {
188+
"babelConfig": {
189+
"configFile": "path/to/babel.config.js"
190+
}
191+
}
192+
}
193+
}
194+
}
195+
```
196+
88197
### Supported template languages
89198

90199
- **pug** (`lang="pug"`)

lib/load-babel-config.js

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ const path = require('path')
55
const { readFileSync, existsSync } = require('fs')
66

77
module.exports = function getBabelConfig (vueJestConfig, filePath) {
8+
const find = () => {
9+
const { file, config } = findBabelConfig.sync(filePath || process.cwd())
10+
11+
if (!file) {
12+
logger.info('no .babelrc found, skipping babel compilation')
13+
cache.set('babel-config', false)
14+
return
15+
}
16+
17+
return config
18+
}
819
const cachedConfig = cache.get('babel-config')
920
if (cachedConfig) {
1021
return cachedConfig
@@ -17,19 +28,33 @@ module.exports = function getBabelConfig (vueJestConfig, filePath) {
1728
babelConfig = JSON.parse(readFileSync(vueJestConfig.babelRcFile))
1829
} else if (existsSync('babel.config.js')) {
1930
babelConfig = require(path.resolve('babel.config.js'))
20-
} else {
21-
const { file, config } = findBabelConfig.sync(filePath || process.cwd())
22-
23-
if (!file) {
24-
logger.info('no .babelrc found, skipping babel compilation')
25-
cache.set('babel-config', false)
26-
return
31+
} else if (vueJestConfig.hasOwnProperty('babelConfig')) {
32+
switch (typeof vueJestConfig.babelConfig) {
33+
case 'string':
34+
// a path to a config file is being passed in; load it
35+
babelConfig = require(vueJestConfig.babelConfig)
36+
break
37+
case 'boolean':
38+
// if babelConfig is true, search for it. If false, will end up
39+
// returning undefined which results in no babel processing
40+
if (vueJestConfig.babelConfig === true) {
41+
babelConfig = find()
42+
}
43+
break
44+
case 'object':
45+
default:
46+
// support for inline babel options
47+
babelConfig = vueJestConfig.babelConfig
48+
break
2749
}
50+
} else {
51+
babelConfig = find()
52+
}
2853

29-
babelConfig = config
54+
if (babelConfig) {
55+
cache.set('babel-config', babelConfig)
3056
}
3157

32-
cache.set('babel-config', babelConfig)
3358
return babelConfig
3459
}
3560
}

test/load-babel-config.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import findBabelConfig from 'find-babel-config'
12
import loadBabelConfig from '../lib/load-babel-config'
23
import { resolve } from 'path'
34
import {
@@ -103,4 +104,54 @@ describe('load-babel-config.js', () => {
103104
expect(babelConfig).toEqual(config)
104105
unlinkSync(babelConfigPath)
105106
})
107+
108+
describe('babelConfig option', () => {
109+
it('supports a path to a babel configuration file', () => {
110+
const babelConfigPath = resolve(__dirname, '../some-babel-config.js')
111+
const config = {
112+
plugins: ['foo']
113+
}
114+
writeFileSync(babelConfigPath, `module.exports = ${JSON.stringify(config)}`)
115+
const babelConfig = loadBabelConfig({
116+
babelConfig: babelConfigPath
117+
})
118+
expect(babelConfig).toEqual(config)
119+
})
120+
121+
it('supports a boolean indicating whether or not to search for babel config', () => {
122+
const config = {
123+
plugins: ['foo']
124+
}
125+
findBabelConfig.sync = jest.fn(() => ({ file: true, config }))
126+
const noBabelConfig = loadBabelConfig({
127+
babelConfig: false
128+
})
129+
expect(findBabelConfig.sync).not.toHaveBeenCalled()
130+
expect(noBabelConfig).toBeUndefined()
131+
132+
const babelConfig = loadBabelConfig({
133+
babelConfig: true
134+
})
135+
expect(findBabelConfig.sync).toHaveBeenCalled()
136+
expect(babelConfig).toEqual(config)
137+
findBabelConfig.sync.mockRestore()
138+
})
139+
140+
it('supports a babel configuration object', () => {
141+
const config = {
142+
plugins: ['foo']
143+
}
144+
const babelConfig = loadBabelConfig({
145+
babelConfig: config
146+
})
147+
expect(babelConfig).toEqual(config)
148+
})
149+
150+
it('defaults to searching for babel config if option is not provided', () => {
151+
findBabelConfig.sync = jest.fn(() => ({}))
152+
loadBabelConfig({})
153+
expect(findBabelConfig.sync).toHaveBeenCalled()
154+
findBabelConfig.sync.mockRestore()
155+
})
156+
})
106157
})

0 commit comments

Comments
 (0)