-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(create-gatsby): add test for init starter in create-gatsby
- Loading branch information
mfrachet
committed
Dec 1, 2020
1 parent
5812c81
commit 7061b12
Showing
4 changed files
with
199 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { execSync } from "child_process" | ||
import execa from "execa" | ||
import fs from "fs-extra" | ||
import path from "path" | ||
import { initStarter } from "../init-starter" | ||
import { reporter } from "../reporter" | ||
|
||
jest.mock(`../utils`) | ||
jest.mock(`execa`) | ||
jest.mock(`child_process`) | ||
jest.mock(`fs-extra`) | ||
jest.mock(`path`) | ||
jest.mock(`../reporter`) | ||
jest.mock(`../get-config-store`, () => { | ||
return { | ||
getConfigStore: (): unknown => { | ||
return { | ||
items: {}, | ||
set(key: string, value: unknown): void { | ||
this.items[key] = value | ||
}, | ||
get(key: string): unknown { | ||
return this.items[key] | ||
}, | ||
|
||
__reset(): void { | ||
this.items = {} | ||
}, | ||
} | ||
}, | ||
} | ||
}) | ||
|
||
describe(`init-starter`, () => { | ||
beforeEach(() => { | ||
process.chdir = jest.fn() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe(`initStarter / cloning`, () => { | ||
it(`reports an error when it s not possible to clone the repo`, async () => { | ||
;(path as any).join.mockImplementation(() => `/somewhere-here`) | ||
;(execa as any).mockImplementation(() => { | ||
throw new Error(`Not possible to clone the repo`) | ||
}) | ||
|
||
try { | ||
await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) | ||
} catch (e) { | ||
expect(execa).toBeCalledWith(`git`, [ | ||
`clone`, | ||
`gatsby-starter-hello-world`, | ||
`--recursive`, | ||
`--depth=1`, | ||
`--quiet`, | ||
]) | ||
expect(reporter.panic).toBeCalledWith(`Not possible to clone the repo`) | ||
expect(reporter.success).not.toBeCalledWith( | ||
`Created site from template` | ||
) | ||
expect(fs.remove).toBeCalledWith(`/somewhere-here`) | ||
} | ||
}) | ||
|
||
it(`reports a success when everything is going ok`, async () => { | ||
;(path as any).join.mockImplementation(() => `/somewhere-here`) | ||
;(execa as any).mockImplementation(() => Promise.resolve()) | ||
;(fs as any).readJSON.mockImplementation(() => { | ||
return { name: `gatsby-project` } | ||
}) | ||
|
||
await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) | ||
|
||
expect(execa).toBeCalledWith(`git`, [ | ||
`clone`, | ||
`gatsby-starter-hello-world`, | ||
`--recursive`, | ||
`--depth=1`, | ||
`--quiet`, | ||
]) | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(reporter.success).toBeCalledWith(`Created site from template`) | ||
expect(fs.remove).toBeCalledWith(`/somewhere-here`) | ||
}) | ||
}) | ||
|
||
describe(`initStarter / install`, () => { | ||
it(`process package installation with yarn`, async () => { | ||
process.env.npm_config_user_agent = `yarn` | ||
;(path as any).join.mockImplementation(() => `/somewhere-here`) | ||
;(execa as any).mockImplementation(() => Promise.resolve()) | ||
;(fs as any).readJSON.mockImplementation(() => { | ||
return { name: `gatsby-project` } | ||
}) | ||
|
||
await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) | ||
|
||
expect(fs.remove).toBeCalledWith(`package-lock.json`) | ||
expect(reporter.success).toBeCalledWith(`Installed plugins`) | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(execa).toBeCalledWith(`yarnpkg`, [`--silent`], { | ||
stderr: `inherit`, | ||
}) | ||
}) | ||
|
||
it(`process package installation with NPM`, async () => { | ||
process.env.npm_config_user_agent = `npm` | ||
;(path as any).join.mockImplementation(() => `/somewhere-here`) | ||
;(execa as any).mockImplementation(() => Promise.resolve()) | ||
;(fs as any).readJSON.mockImplementation(() => { | ||
return { name: `gatsby-project` } | ||
}) | ||
|
||
await initStarter(`gatsby-starter-hello-world`, `./somewhere`, [ | ||
`one-package`, | ||
]) | ||
|
||
expect(fs.remove).toBeCalledWith(`yarn.lock`) | ||
expect(reporter.success).toBeCalledWith(`Installed Gatsby`) | ||
expect(reporter.success).toBeCalledWith(`Installed plugins`) | ||
expect(reporter.panic).not.toBeCalled() | ||
expect(execa).toBeCalledWith( | ||
`npm`, | ||
[`install`, `--loglevel`, `error`, `--color`, `always`], | ||
{ stderr: `inherit` } | ||
) | ||
expect(execa).toBeCalledWith( | ||
`npm`, | ||
[`install`, `--loglevel`, `error`, `--color`, `always`, `one-package`], | ||
{ stderr: `inherit` } | ||
) | ||
}) | ||
|
||
it(`gently informs the user that yarn is not available when trying to use it`, async () => { | ||
process.env.npm_config_user_agent = `yarn` | ||
;(execSync as any).mockImplementation(() => { | ||
throw new Error(`Something wrong occured when trying to use yarn`) | ||
}) | ||
;(path as any).join.mockImplementation(() => `/somewhere-here`) | ||
;(execa as any).mockImplementation(() => Promise.resolve()) | ||
;(fs as any).readJSON.mockImplementation(() => { | ||
return { name: `gatsby-project` } | ||
}) | ||
|
||
await initStarter(`gatsby-starter-hello-world`, `./somewhere`, [ | ||
`one-package`, | ||
]) | ||
|
||
expect(reporter.info).toBeCalledWith( | ||
`Woops! Yarn doesn't seem be installed on your machine. You can install it on https://yarnpkg.com/getting-started/install. As a fallback, we will run the next steps with npm.` | ||
) | ||
}) | ||
}) | ||
}) |
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
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,12 @@ | ||
const ESC = `\u001b` | ||
|
||
export const clearLine = (count = 1): Promise<boolean> => | ||
new Promise(resolve => { | ||
// First move the cursor up one line... | ||
process.stderr.moveCursor(0, -count, () => { | ||
// ... then clear that line. This is the ANSI escape sequence for "clear whole line" | ||
// List of escape sequences: http://ascii-table.com/ansi-escape-sequences.php | ||
process.stderr.write(`${ESC}[2K`) | ||
resolve() | ||
}) | ||
}) |