Skip to content

Commit 3779201

Browse files
test(babel-preset-gatsby-package): add test for production mode (#12177)
* test(babel-preset-gatsby-package): add test for production browser targets * refactor: use snapshots * refactor: use beforeAll/afterAll hooks to deal with env
1 parent c15a595 commit 3779201

File tree

4 files changed

+244
-148
lines changed

4 files changed

+244
-148
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`babel-preset-gatsby-package in browser mode specifies proper presets 1`] = `
4+
Array [
5+
Array [
6+
"@babel/preset-env",
7+
Object {
8+
"debug": false,
9+
"loose": true,
10+
"modules": "commonjs",
11+
"shippedProposals": true,
12+
"targets": Object {
13+
"browsers": Array [
14+
"last 2 versions",
15+
"not ie <= 11",
16+
"not android 4.4.3",
17+
],
18+
},
19+
"useBuiltIns": false,
20+
},
21+
],
22+
Array [
23+
"@babel/preset-react",
24+
Object {
25+
"development": true,
26+
},
27+
],
28+
"@babel/preset-flow",
29+
]
30+
`;
31+
32+
exports[`babel-preset-gatsby-package in browser mode specifies proper presets for debugging 1`] = `
33+
Array [
34+
Array [
35+
"@babel/preset-env",
36+
Object {
37+
"debug": true,
38+
"loose": true,
39+
"modules": "commonjs",
40+
"shippedProposals": true,
41+
"targets": Object {
42+
"browsers": Array [
43+
"last 2 versions",
44+
"not ie <= 11",
45+
"not android 4.4.3",
46+
],
47+
},
48+
"useBuiltIns": false,
49+
},
50+
],
51+
Array [
52+
"@babel/preset-react",
53+
Object {
54+
"development": true,
55+
},
56+
],
57+
"@babel/preset-flow",
58+
]
59+
`;
60+
61+
exports[`babel-preset-gatsby-package in browser mode specifies the proper plugins 1`] = `
62+
Array [
63+
"@babel/plugin-proposal-class-properties",
64+
"@babel/plugin-proposal-optional-chaining",
65+
"@babel/plugin-transform-runtime",
66+
"@babel/plugin-syntax-dynamic-import",
67+
]
68+
`;
69+
70+
exports[`babel-preset-gatsby-package in node mode specifies proper presets 1`] = `
71+
Array [
72+
Array [
73+
"@babel/preset-env",
74+
Object {
75+
"debug": false,
76+
"loose": true,
77+
"modules": "commonjs",
78+
"shippedProposals": true,
79+
"targets": Object {
80+
"node": "current",
81+
},
82+
"useBuiltIns": "entry",
83+
},
84+
],
85+
Array [
86+
"@babel/preset-react",
87+
Object {
88+
"development": true,
89+
},
90+
],
91+
"@babel/preset-flow",
92+
]
93+
`;
94+
95+
exports[`babel-preset-gatsby-package in node mode specifies proper presets for debugging 1`] = `
96+
Array [
97+
Array [
98+
"@babel/preset-env",
99+
Object {
100+
"debug": true,
101+
"loose": true,
102+
"modules": "commonjs",
103+
"shippedProposals": true,
104+
"targets": Object {
105+
"node": "current",
106+
},
107+
"useBuiltIns": "entry",
108+
},
109+
],
110+
Array [
111+
"@babel/preset-react",
112+
Object {
113+
"development": true,
114+
},
115+
],
116+
"@babel/preset-flow",
117+
]
118+
`;
119+
120+
exports[`babel-preset-gatsby-package in node mode specifies the proper plugins 1`] = `
121+
Array [
122+
"@babel/plugin-proposal-class-properties",
123+
"@babel/plugin-proposal-optional-chaining",
124+
"@babel/plugin-transform-runtime",
125+
"@babel/plugin-syntax-dynamic-import",
126+
]
127+
`;
128+
129+
exports[`babel-preset-gatsby-package in production mode specifies proper presets for browser mode 1`] = `
130+
Array [
131+
Array [
132+
"@babel/preset-env",
133+
Object {
134+
"debug": false,
135+
"loose": true,
136+
"modules": "commonjs",
137+
"shippedProposals": true,
138+
"targets": Object {
139+
"browsers": Array [
140+
"last 4 versions",
141+
"safari >= 7",
142+
"ie >= 9",
143+
],
144+
},
145+
"useBuiltIns": false,
146+
},
147+
],
148+
Array [
149+
"@babel/preset-react",
150+
Object {
151+
"development": false,
152+
},
153+
],
154+
"@babel/preset-flow",
155+
]
156+
`;
157+
158+
exports[`babel-preset-gatsby-package in production mode specifies proper presets for node mode 1`] = `
159+
Array [
160+
Array [
161+
"@babel/preset-env",
162+
Object {
163+
"debug": false,
164+
"loose": true,
165+
"modules": "commonjs",
166+
"shippedProposals": true,
167+
"targets": Object {
168+
"node": 6,
169+
},
170+
"useBuiltIns": "entry",
171+
},
172+
],
173+
Array [
174+
"@babel/preset-react",
175+
Object {
176+
"development": false,
177+
},
178+
],
179+
"@babel/preset-flow",
180+
]
181+
`;
Lines changed: 59 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,62 @@
11
const preset = require(`../`)
2-
const path = require(`path`)
32

4-
it(`Specifies proper presets and plugins in Node mode`, () => {
5-
const { presets, plugins } = preset()
6-
7-
expect(presets).toEqual([
8-
[
9-
expect.stringContaining(path.join(`@babel`, `preset-env`)),
10-
{
11-
debug: false,
12-
loose: true,
13-
modules: `commonjs`,
14-
shippedProposals: true,
15-
targets: {
16-
node: `current`,
17-
},
18-
useBuiltIns: `entry`,
19-
},
20-
],
21-
[
22-
expect.stringContaining(path.join(`@babel`, `preset-react`)),
23-
{ development: true },
24-
],
25-
expect.stringContaining(path.join(`@babel`, `preset-flow`)),
26-
])
27-
expect(plugins).toEqual([
28-
expect.stringContaining(
29-
path.join(`@babel`, `plugin-proposal-class-properties`)
30-
),
31-
expect.stringContaining(
32-
path.join(`@babel`, `plugin-proposal-optional-chaining`)
33-
),
34-
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
35-
expect.stringContaining(
36-
path.join(`@babel`, `plugin-syntax-dynamic-import`)
37-
),
38-
])
39-
})
40-
41-
it(`Specifies proper presets and plugins in debug Node mode`, () => {
42-
const { presets, plugins } = preset(null, { debug: true })
43-
44-
expect(presets).toEqual([
45-
[
46-
expect.stringContaining(path.join(`@babel`, `preset-env`)),
47-
{
48-
debug: true,
49-
loose: true,
50-
modules: `commonjs`,
51-
shippedProposals: true,
52-
targets: {
53-
node: `current`,
54-
},
55-
useBuiltIns: `entry`,
56-
},
57-
],
58-
[
59-
expect.stringContaining(path.join(`@babel`, `preset-react`)),
60-
{ development: true },
61-
],
62-
expect.stringContaining(path.join(`@babel`, `preset-flow`)),
63-
])
64-
expect(plugins).toEqual([
65-
expect.stringContaining(
66-
path.join(`@babel`, `plugin-proposal-class-properties`)
67-
),
68-
expect.stringContaining(
69-
path.join(`@babel`, `plugin-proposal-optional-chaining`)
70-
),
71-
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
72-
expect.stringContaining(
73-
path.join(`@babel`, `plugin-syntax-dynamic-import`)
74-
),
75-
])
76-
})
77-
78-
it(`Specifies proper presets and plugins in browser mode`, () => {
79-
const { presets, plugins } = preset(null, { browser: true })
80-
81-
expect(presets).toEqual([
82-
[
83-
expect.stringContaining(path.join(`@babel`, `preset-env`)),
84-
{
85-
debug: false,
86-
loose: true,
87-
modules: `commonjs`,
88-
shippedProposals: true,
89-
targets: {
90-
browsers: [`last 2 versions`, `not ie <= 11`, `not android 4.4.3`],
91-
},
92-
useBuiltIns: false,
93-
},
94-
],
95-
[
96-
expect.stringContaining(path.join(`@babel`, `preset-react`)),
97-
{ development: true },
98-
],
99-
expect.stringContaining(path.join(`@babel`, `preset-flow`)),
100-
])
101-
expect(plugins).toEqual([
102-
expect.stringContaining(
103-
path.join(`@babel`, `plugin-proposal-class-properties`)
104-
),
105-
expect.stringContaining(
106-
path.join(`@babel`, `plugin-proposal-optional-chaining`)
107-
),
108-
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
109-
expect.stringContaining(
110-
path.join(`@babel`, `plugin-syntax-dynamic-import`)
111-
),
112-
])
113-
})
114-
115-
it(`Specifies proper presets and plugins in debug browser mode`, () => {
116-
const { presets, plugins } = preset(null, { browser: true, debug: true })
117-
118-
expect(presets).toEqual([
119-
[
120-
expect.stringContaining(path.join(`@babel`, `preset-env`)),
121-
{
122-
debug: true,
123-
loose: true,
124-
modules: `commonjs`,
125-
shippedProposals: true,
126-
targets: {
127-
browsers: [`last 2 versions`, `not ie <= 11`, `not android 4.4.3`],
128-
},
129-
useBuiltIns: false,
130-
},
131-
],
132-
[
133-
expect.stringContaining(path.join(`@babel`, `preset-react`)),
134-
{ development: true },
135-
],
136-
expect.stringContaining(path.join(`@babel`, `preset-flow`)),
137-
])
138-
expect(plugins).toEqual([
139-
expect.stringContaining(
140-
path.join(`@babel`, `plugin-proposal-class-properties`)
141-
),
142-
expect.stringContaining(
143-
path.join(`@babel`, `plugin-proposal-optional-chaining`)
144-
),
145-
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
146-
expect.stringContaining(
147-
path.join(`@babel`, `plugin-syntax-dynamic-import`)
148-
),
149-
])
3+
jest.mock(`../resolver`, () => jest.fn(moduleName => moduleName))
4+
5+
describe(`babel-preset-gatsby-package`, () => {
6+
describe(`in node mode`, () => {
7+
it(`specifies the proper plugins`, () => {
8+
const { plugins } = preset()
9+
expect(plugins).toMatchSnapshot()
10+
})
11+
12+
it(`specifies proper presets`, () => {
13+
const { presets } = preset()
14+
expect(presets).toMatchSnapshot()
15+
})
16+
17+
it(`specifies proper presets for debugging`, () => {
18+
const { presets } = preset(null, { debug: true })
19+
expect(presets).toMatchSnapshot()
20+
})
21+
})
22+
23+
describe(`in browser mode`, () => {
24+
it(`specifies the proper plugins`, () => {
25+
const { plugins } = preset(null, { browser: true })
26+
expect(plugins).toMatchSnapshot()
27+
})
28+
29+
it(`specifies proper presets`, () => {
30+
const { presets } = preset(null, { browser: true })
31+
expect(presets).toMatchSnapshot()
32+
})
33+
34+
it(`specifies proper presets for debugging`, () => {
35+
const { presets } = preset(null, { browser: true, debug: true })
36+
expect(presets).toMatchSnapshot()
37+
})
38+
})
39+
40+
describe(`in production mode`, () => {
41+
let env
42+
43+
beforeAll(() => {
44+
env = process.env.BABEL_ENV
45+
process.env.BABEL_ENV = `production`
46+
})
47+
48+
afterAll(() => {
49+
process.env.BABEL_ENV = env
50+
})
51+
52+
it(`specifies proper presets for node mode`, () => {
53+
const { presets } = preset(null)
54+
expect(presets).toMatchSnapshot()
55+
})
56+
57+
it(`specifies proper presets for browser mode`, () => {
58+
const { presets } = preset(null, { browser: true })
59+
expect(presets).toMatchSnapshot()
60+
})
61+
})
15062
})

packages/babel-preset-gatsby-package/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const r = m => require.resolve(m)
1+
const r = require(`./resolver`)
22

33
function preset(context, options = {}) {
44
const { browser = false, debug = false } = options

0 commit comments

Comments
 (0)