Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1a1d560

Browse filesBrowse files
author
Justin Helmer
committed
feat: update to support babel config bool/string/object
1 parent d5ba225 commit 1a1d560
Copy full SHA for 1a1d560

File tree

Expand file treeCollapse file tree

3 files changed

+197
-11
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+197
-11
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+111-2Lines 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

Copy file name to clipboardExpand all lines: lib/load-babel-config.js
+35-9Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ const cache = require('./cache')
44
const path = require('path')
55
const { readFileSync, existsSync } = require('fs')
66

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

29-
babelConfig = config
55+
if (babelConfig) {
56+
cache.set('babel-config', babelConfig)
3057
}
3158

32-
cache.set('babel-config', babelConfig)
3359
return babelConfig
3460
}
3561
}

‎test/load-babel-config.spec.js

Copy file name to clipboardExpand all lines: test/load-babel-config.spec.js
+51Lines 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 {
@@ -75,4 +76,54 @@ describe('load-babel-config.js', () => {
7576
expect(babelConfig).toEqual(config)
7677
unlinkSync(babelConfigPath)
7778
})
79+
80+
describe('babelConfig option', () => {
81+
it('supports a path to a babel configuration file', () => {
82+
const babelConfigPath = resolve(__dirname, '../some-babel-config.js')
83+
const config = {
84+
plugins: ['foo']
85+
}
86+
writeFileSync(babelConfigPath, `module.exports = ${JSON.stringify(config)}`)
87+
const babelConfig = loadBabelConfig({
88+
babelConfig: babelConfigPath
89+
})
90+
expect(babelConfig).toEqual(config)
91+
})
92+
93+
it('supports a boolean indicating whether or not to search for babel config', () => {
94+
const config = {
95+
plugins: ['foo']
96+
}
97+
findBabelConfig.sync = jest.fn(() => ({ file: true, config }))
98+
const noBabelConfig = loadBabelConfig({
99+
babelConfig: false
100+
})
101+
expect(findBabelConfig.sync).not.toHaveBeenCalled()
102+
expect(noBabelConfig).toBeUndefined()
103+
104+
const babelConfig = loadBabelConfig({
105+
babelConfig: true
106+
})
107+
expect(findBabelConfig.sync).toHaveBeenCalled()
108+
expect(babelConfig).toEqual(config)
109+
findBabelConfig.sync.mockRestore()
110+
})
111+
112+
it('supports a babel configuration object', () => {
113+
const config = {
114+
plugins: ['foo']
115+
}
116+
const babelConfig = loadBabelConfig({
117+
babelConfig: config
118+
})
119+
expect(babelConfig).toEqual(config)
120+
})
121+
122+
it('defaults to searching for babel config if option is not provided', () => {
123+
findBabelConfig.sync = jest.fn(() => ({}))
124+
loadBabelConfig({})
125+
expect(findBabelConfig.sync).toHaveBeenCalled()
126+
findBabelConfig.sync.mockRestore()
127+
})
128+
})
78129
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.