|
| 1 | +import { execSync } from "child_process" |
| 2 | +import execa from "execa" |
| 3 | +import fs from "fs-extra" |
| 4 | +import path from "path" |
| 5 | +import { initStarter } from "../init-starter" |
| 6 | +import { reporter } from "../reporter" |
| 7 | + |
| 8 | +jest.mock(`../utils`) |
| 9 | +jest.mock(`execa`) |
| 10 | +jest.mock(`child_process`) |
| 11 | +jest.mock(`fs-extra`) |
| 12 | +jest.mock(`path`) |
| 13 | +jest.mock(`../reporter`) |
| 14 | +jest.mock(`../get-config-store`, () => { |
| 15 | + return { |
| 16 | + getConfigStore: (): unknown => { |
| 17 | + return { |
| 18 | + items: {}, |
| 19 | + set(key: string, value: unknown): void { |
| 20 | + this.items[key] = value |
| 21 | + }, |
| 22 | + get(key: string): unknown { |
| 23 | + return this.items[key] |
| 24 | + }, |
| 25 | + |
| 26 | + __reset(): void { |
| 27 | + this.items = {} |
| 28 | + }, |
| 29 | + } |
| 30 | + }, |
| 31 | + } |
| 32 | +}) |
| 33 | + |
| 34 | +describe(`init-starter`, () => { |
| 35 | + beforeEach(() => { |
| 36 | + process.chdir = jest.fn() |
| 37 | + }) |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + jest.resetAllMocks() |
| 41 | + }) |
| 42 | + |
| 43 | + describe(`initStarter / cloning`, () => { |
| 44 | + it(`reports an error when it s not possible to clone the repo`, async () => { |
| 45 | + ;(path as any).join.mockImplementation(() => `/somewhere-here`) |
| 46 | + ;(execa as any).mockImplementation(() => { |
| 47 | + throw new Error(`Not possible to clone the repo`) |
| 48 | + }) |
| 49 | + |
| 50 | + try { |
| 51 | + await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) |
| 52 | + } catch (e) { |
| 53 | + expect(execa).toBeCalledWith(`git`, [ |
| 54 | + `clone`, |
| 55 | + `gatsby-starter-hello-world`, |
| 56 | + `--recursive`, |
| 57 | + `--depth=1`, |
| 58 | + `--quiet`, |
| 59 | + ]) |
| 60 | + expect(reporter.panic).toBeCalledWith(`Not possible to clone the repo`) |
| 61 | + expect(reporter.success).not.toBeCalledWith( |
| 62 | + `Created site from template` |
| 63 | + ) |
| 64 | + expect(fs.remove).toBeCalledWith(`/somewhere-here`) |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + it(`reports a success when everything is going ok`, async () => { |
| 69 | + ;(path as any).join.mockImplementation(() => `/somewhere-here`) |
| 70 | + ;(execa as any).mockImplementation(() => Promise.resolve()) |
| 71 | + ;(fs as any).readJSON.mockImplementation(() => { |
| 72 | + return { name: `gatsby-project` } |
| 73 | + }) |
| 74 | + |
| 75 | + await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) |
| 76 | + |
| 77 | + expect(execa).toBeCalledWith(`git`, [ |
| 78 | + `clone`, |
| 79 | + `gatsby-starter-hello-world`, |
| 80 | + `--recursive`, |
| 81 | + `--depth=1`, |
| 82 | + `--quiet`, |
| 83 | + ]) |
| 84 | + expect(reporter.panic).not.toBeCalled() |
| 85 | + expect(reporter.success).toBeCalledWith(`Created site from template`) |
| 86 | + expect(fs.remove).toBeCalledWith(`/somewhere-here`) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe(`initStarter / install`, () => { |
| 91 | + it(`process package installation with yarn`, async () => { |
| 92 | + process.env.npm_config_user_agent = `yarn` |
| 93 | + ;(path as any).join.mockImplementation(() => `/somewhere-here`) |
| 94 | + ;(execa as any).mockImplementation(() => Promise.resolve()) |
| 95 | + ;(fs as any).readJSON.mockImplementation(() => { |
| 96 | + return { name: `gatsby-project` } |
| 97 | + }) |
| 98 | + |
| 99 | + await initStarter(`gatsby-starter-hello-world`, `./somewhere`, []) |
| 100 | + |
| 101 | + expect(fs.remove).toBeCalledWith(`package-lock.json`) |
| 102 | + expect(reporter.success).toBeCalledWith(`Installed plugins`) |
| 103 | + expect(reporter.panic).not.toBeCalled() |
| 104 | + expect(execa).toBeCalledWith(`yarnpkg`, [`--silent`], { |
| 105 | + stderr: `inherit`, |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + it(`process package installation with NPM`, async () => { |
| 110 | + process.env.npm_config_user_agent = `npm` |
| 111 | + ;(path as any).join.mockImplementation(() => `/somewhere-here`) |
| 112 | + ;(execa as any).mockImplementation(() => Promise.resolve()) |
| 113 | + ;(fs as any).readJSON.mockImplementation(() => { |
| 114 | + return { name: `gatsby-project` } |
| 115 | + }) |
| 116 | + |
| 117 | + await initStarter(`gatsby-starter-hello-world`, `./somewhere`, [ |
| 118 | + `one-package`, |
| 119 | + ]) |
| 120 | + |
| 121 | + expect(fs.remove).toBeCalledWith(`yarn.lock`) |
| 122 | + expect(reporter.success).toBeCalledWith(`Installed Gatsby`) |
| 123 | + expect(reporter.success).toBeCalledWith(`Installed plugins`) |
| 124 | + expect(reporter.panic).not.toBeCalled() |
| 125 | + expect(execa).toBeCalledWith( |
| 126 | + `npm`, |
| 127 | + [`install`, `--loglevel`, `error`, `--color`, `always`], |
| 128 | + { stderr: `inherit` } |
| 129 | + ) |
| 130 | + expect(execa).toBeCalledWith( |
| 131 | + `npm`, |
| 132 | + [`install`, `--loglevel`, `error`, `--color`, `always`, `one-package`], |
| 133 | + { stderr: `inherit` } |
| 134 | + ) |
| 135 | + }) |
| 136 | + |
| 137 | + it(`gently informs the user that yarn is not available when trying to use it`, async () => { |
| 138 | + process.env.npm_config_user_agent = `yarn` |
| 139 | + ;(execSync as any).mockImplementation(() => { |
| 140 | + throw new Error(`Something wrong occured when trying to use yarn`) |
| 141 | + }) |
| 142 | + ;(path as any).join.mockImplementation(() => `/somewhere-here`) |
| 143 | + ;(execa as any).mockImplementation(() => Promise.resolve()) |
| 144 | + ;(fs as any).readJSON.mockImplementation(() => { |
| 145 | + return { name: `gatsby-project` } |
| 146 | + }) |
| 147 | + |
| 148 | + await initStarter(`gatsby-starter-hello-world`, `./somewhere`, [ |
| 149 | + `one-package`, |
| 150 | + ]) |
| 151 | + |
| 152 | + expect(reporter.info).toBeCalledWith( |
| 153 | + `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.` |
| 154 | + ) |
| 155 | + }) |
| 156 | + }) |
| 157 | +}) |
0 commit comments