-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-config.test.js
84 lines (62 loc) · 2.21 KB
/
gatsby-config.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import path from "path"
import fs from "fs-extra"
import execa from "execa"
jest.setTimeout(100000)
const fixtureRoot = path.resolve(__dirname, `fixtures`)
const siteRoot = path.resolve(__dirname, `..`)
const fixturePath = {
cjs: path.join(fixtureRoot, `gatsby-config.js`),
esm: path.join(fixtureRoot, `gatsby-config.mjs`),
}
const configPath = {
cjs: path.join(siteRoot, `gatsby-config.js`),
esm: path.join(siteRoot, `gatsby-config.mjs`),
}
const localPluginFixtureDir = path.join(fixtureRoot, `plugins`)
const localPluginTargetDir = path.join(siteRoot, `plugins`)
const gatsbyBin = path.join(`node_modules`, `gatsby`, `cli.js`)
async function build() {
const { stdout } = await execa(process.execPath, [gatsbyBin, `build`], {
env: {
...process.env,
NODE_ENV: `production`,
},
})
return stdout
}
// Tests include multiple assertions since running multiple builds is time consuming
describe(`gatsby-config.js`, () => {
afterEach(() => {
fs.rmSync(configPath.cjs)
})
it(`works with required CJS modules`, async () => {
await fs.copyFile(fixturePath.cjs, configPath.cjs)
const stdout = await build()
// Build succeeded
expect(stdout).toContain(`Done building`)
// Requires work
expect(stdout).toContain(`hello-default-cjs`)
expect(stdout).toContain(`hello-named-cjs`)
})
})
describe(`gatsby-config.mjs`, () => {
afterEach(async () => {
await fs.rm(configPath.esm)
await fs.rm(localPluginTargetDir, { recursive: true })
})
it(`works with imported ESM modules`, async () => {
await fs.copyFile(fixturePath.esm, configPath.esm)
await fs.ensureDir(localPluginTargetDir)
await fs.copy(localPluginFixtureDir, localPluginTargetDir)
const stdout = await build()
// Build succeeded
expect(stdout).toContain(`Done building`)
// Imports work
expect(stdout).toContain(`hello-default-esm`)
expect(stdout).toContain(`hello-named-esm`)
// Local plugin gatsby-config.mjs works
expect(stdout).toContain(`a-local-plugin-gatsby-config-mjs`)
// Local plugin with an esm module passed via options works, this implicitly tests gatsby-node.mjs too
expect(stdout).toContain(`a-local-plugin-using-passed-esm-module`)
})
})