Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-core-utils,gatsby-cli): Allow write to gatsby-config.ts (#…
…35074) Co-authored-by: Lennart <lekoarts@gmail.com>
- Loading branch information
Showing
13 changed files
with
613 additions
and
166 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
104 changes: 104 additions & 0 deletions
104
packages/gatsby-cli/src/__tests__/handlers/__snapshots__/plugin-add.ts.snap
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,104 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`addPlugins gatsby-config.js should not write with no plugins 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\`, | ||
}, | ||
plugins: [], | ||
} | ||
" | ||
`; | ||
exports[`addPlugins gatsby-config.js should write a plugin with options 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [{ | ||
resolve: 'gatsby-plugin-hello', | ||
options: { | ||
\\"greet\\": true | ||
} | ||
}] | ||
};" | ||
`; | ||
exports[`addPlugins gatsby-config.js should write a single plugin 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\"] | ||
};" | ||
`; | ||
exports[`addPlugins gatsby-config.js should write multiple plugins 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\", \\"gatsby-plugin-world\\"] | ||
};" | ||
`; | ||
exports[`addPlugins gatsby-config.ts should not write with no plugins 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\" | ||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\`, | ||
}, | ||
plugins: [], | ||
} | ||
export default config | ||
" | ||
`; | ||
exports[`addPlugins gatsby-config.ts should write a plugin with options 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [{ | ||
resolve: 'gatsby-plugin-hello', | ||
options: { | ||
\\"greet\\": true | ||
} | ||
}] | ||
}; | ||
export default config; | ||
" | ||
`; | ||
exports[`addPlugins gatsby-config.ts should write a single plugin 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\"] | ||
}; | ||
export default config; | ||
" | ||
`; | ||
exports[`addPlugins gatsby-config.ts should write multiple plugins 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\", \\"gatsby-plugin-world\\"] | ||
}; | ||
export default config; | ||
" | ||
`; |
120 changes: 120 additions & 0 deletions
120
packages/gatsby-cli/src/__tests__/handlers/plugin-add.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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { ensureDir, copyFile, readFile, rm } from "fs-extra" | ||
import { join } from "path" | ||
import { addPlugins } from "../../handlers/plugin-add" | ||
|
||
/** | ||
* Copy files from minimal starters instead of testing against static gatsby-configs | ||
* in fixtues so that these break if we change the starter configs in a breaking way. | ||
* | ||
* Not using `jest.each` since I find that much harder to read and debug. | ||
* @see {@link https://jestjs.io/docs/api#testeachtablename-fn-timeout} | ||
*/ | ||
|
||
const root = join(__dirname, `../../../../..`) | ||
const fixtures = join(__dirname, `../fixtures`) | ||
const config = { | ||
js: { | ||
starter: `${root}/starters/gatsby-starter-minimal/gatsby-config.js`, | ||
fixture: `${fixtures}/gatsby-config.js`, | ||
}, | ||
ts: { | ||
starter: `${root}/starters/gatsby-starter-minimal-ts/gatsby-config.ts`, | ||
fixture: `${fixtures}/gatsby-config.ts`, | ||
}, | ||
} | ||
const plugin = { | ||
hello: `gatsby-plugin-hello`, | ||
world: `gatsby-plugin-world`, | ||
} | ||
|
||
describe(`addPlugins`, () => { | ||
beforeAll(async () => { | ||
await ensureDir(fixtures) | ||
}) | ||
|
||
describe(`gatsby-config.js`, () => { | ||
beforeEach(async () => { | ||
await copyFile(config.js.starter, config.js.fixture) | ||
}) | ||
|
||
afterEach(async () => { | ||
await rm(config.js.fixture) | ||
}) | ||
|
||
it(`should not write with no plugins`, async () => { | ||
await addPlugins([], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a single plugin`, async () => { | ||
await addPlugins([plugin.hello], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write multiple plugins`, async () => { | ||
await addPlugins([plugin.hello, plugin.world], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a plugin with options`, async () => { | ||
await addPlugins( | ||
[plugin.hello], | ||
{ | ||
[plugin.hello]: { | ||
greet: true, | ||
}, | ||
}, | ||
fixtures, | ||
[] | ||
) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe.skip(`gatsby-config.ts`, () => { | ||
beforeEach(async () => { | ||
await copyFile(config.ts.starter, config.ts.fixture) | ||
}) | ||
|
||
afterEach(async () => { | ||
await rm(config.ts.fixture) | ||
}) | ||
|
||
it(`should not write with no plugins`, async () => { | ||
await addPlugins([], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a single plugin`, async () => { | ||
await addPlugins([plugin.hello], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write multiple plugins`, async () => { | ||
await addPlugins([plugin.hello, plugin.world], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a plugin with options`, async () => { | ||
await addPlugins( | ||
[plugin.hello], | ||
{ | ||
[plugin.hello]: { | ||
greet: true, | ||
}, | ||
}, | ||
fixtures, | ||
[] | ||
) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.