-
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.
feat(create-gatsby): TypeScript option (#34611)
Co-authored-by: Lennart <lekoarts@gmail.com> Co-authored-by: Jude Agboola <marvinjudehk@gmail.com>
- Loading branch information
1 parent
01adaff
commit a78ef94
Showing
11 changed files
with
709 additions
and
101 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
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,255 @@ | ||
import { reporter } from "../utils/reporter" | ||
import { initStarter } from "../init-starter" | ||
import { trackCli } from "../tracking" | ||
import { run, DEFAULT_STARTERS } from "../index" | ||
|
||
jest.mock(`../utils/parse-args`) | ||
jest.mock(`enquirer`, () => { | ||
const OriginalEnquirer = jest.requireActual(`enquirer`) | ||
|
||
class MockedEnquirer extends OriginalEnquirer { | ||
constructor() { | ||
super() | ||
// Turns waiting for user input off and autofills with answers | ||
this.options = { show: false, autofill: true } | ||
|
||
// Mock answers | ||
this.answers = { | ||
// First prompt answer | ||
name: `hello-world`, | ||
|
||
// Main question set answers | ||
project: `hello-world`, | ||
language: `js`, | ||
cms: `none`, | ||
styling: `none`, | ||
features: [], | ||
|
||
// Confirmation prompt answer | ||
confirm: true, | ||
} | ||
} | ||
} | ||
return MockedEnquirer | ||
}) | ||
jest.mock(`../utils/reporter`) | ||
jest.mock(`../tracking`, () => { | ||
return { | ||
trackCli: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`../init-starter`, () => { | ||
return { | ||
initStarter: jest.fn(), | ||
getPackageManager: jest.fn(), | ||
gitSetup: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`../install-plugins`, () => { | ||
return { | ||
installPlugins: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`../utils/site-metadata`, () => { | ||
return { | ||
setSiteMetadata: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`../utils/hash`, () => { | ||
return { | ||
sha256: jest.fn(args => args), | ||
md5: jest.fn(args => args), | ||
} | ||
}) | ||
jest.mock(`../utils/question-helpers`, () => { | ||
const originalQuestionHelpers = jest.requireActual( | ||
`../utils/question-helpers` | ||
) | ||
return { | ||
...originalQuestionHelpers, | ||
validateProjectName: jest.fn(() => true), | ||
} | ||
}) | ||
jest.mock(`../components/utils`, () => { | ||
return { | ||
center: jest.fn(args => args), | ||
wrap: jest.fn(args => args), | ||
} | ||
}) | ||
|
||
const dirName = `hello-world` | ||
let parseArgsMock | ||
|
||
describe(`run`, () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
parseArgsMock = require(`../utils/parse-args`).parseArgs | ||
}) | ||
|
||
describe(`no skip flag`, () => { | ||
beforeEach(() => { | ||
parseArgsMock.mockReturnValueOnce({ | ||
flags: { yes: false }, | ||
dirName, | ||
}) | ||
}) | ||
|
||
it(`should welcome the user`, async () => { | ||
await run() | ||
expect(reporter.info).toHaveBeenCalledWith( | ||
expect.stringContaining(`Welcome to Gatsby!`) | ||
) | ||
}) | ||
it(`should communicate setup questions will be asked`, async () => { | ||
await run() | ||
expect(reporter.info).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
`This command will generate a new Gatsby site for you` | ||
) | ||
) | ||
}) | ||
it(`should confirm actions`, async () => { | ||
await run() | ||
expect(reporter.info).toHaveBeenCalledWith( | ||
expect.stringContaining(`Thanks! Here's what we'll now do`) | ||
) | ||
}) | ||
it(`should notify of successful site creation`, async () => { | ||
await run() | ||
expect(reporter.success).toHaveBeenCalledWith( | ||
expect.stringContaining(`Created site`) | ||
) | ||
}) | ||
}) | ||
|
||
describe(`skip flag`, () => { | ||
beforeEach(() => { | ||
parseArgsMock.mockReturnValueOnce({ | ||
flags: { yes: true }, | ||
dirName, | ||
}) | ||
}) | ||
|
||
it(`should welcome the user`, async () => { | ||
await run() | ||
expect(reporter.info).toHaveBeenCalledWith( | ||
expect.stringContaining(`Welcome to Gatsby!`) | ||
) | ||
}) | ||
it(`should not communicate setup questions`, async () => { | ||
await run() | ||
expect(reporter.info).not.toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
`This command will generate a new Gatsby site for you` | ||
) | ||
) | ||
}) | ||
it(`should not confirm actions`, async () => { | ||
await run() | ||
expect(reporter.info).not.toHaveBeenCalledWith( | ||
expect.stringContaining(`Thanks! Here's what we'll now do`) | ||
) | ||
}) | ||
it(`should notify of successful site creation`, async () => { | ||
await run() | ||
expect(reporter.success).toHaveBeenCalledWith( | ||
expect.stringContaining(`Created site`) | ||
) | ||
}) | ||
it(`should use the JS starter by default`, async () => { | ||
await run() | ||
expect(initStarter).toHaveBeenCalledWith( | ||
DEFAULT_STARTERS.js, | ||
dirName, | ||
[], | ||
dirName | ||
) | ||
}) | ||
it(`should track JS was selected as language`, async () => { | ||
await run() | ||
expect(trackCli).toHaveBeenCalledWith(`CREATE_GATSBY_SELECT_OPTION`, { | ||
name: `LANGUAGE`, | ||
valueString: `js`, | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`no ts flag`, () => { | ||
beforeEach(() => { | ||
parseArgsMock.mockReturnValueOnce({ | ||
flags: { ts: false }, | ||
dirName, | ||
}) | ||
}) | ||
|
||
it(`should use the JS starter`, async () => { | ||
await run() | ||
expect(initStarter).toHaveBeenCalledWith( | ||
DEFAULT_STARTERS.js, | ||
dirName, | ||
[], | ||
dirName | ||
) | ||
}) | ||
it(`should track JS was selected as language`, async () => { | ||
await run() | ||
expect(trackCli).toHaveBeenCalledWith(`CREATE_GATSBY_SELECT_OPTION`, { | ||
name: `LANGUAGE`, | ||
valueString: `js`, | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`ts flag`, () => { | ||
beforeEach(() => { | ||
parseArgsMock.mockReturnValueOnce({ | ||
flags: { ts: true }, | ||
dirName, | ||
}) | ||
}) | ||
|
||
it(`should use the TS starter`, async () => { | ||
await run() | ||
expect(initStarter).toHaveBeenCalledWith( | ||
DEFAULT_STARTERS.ts, | ||
dirName, | ||
[], | ||
dirName | ||
) | ||
}) | ||
|
||
it(`should track TS was selected as language`, async () => { | ||
await run() | ||
expect(trackCli).toHaveBeenCalledWith(`CREATE_GATSBY_SELECT_OPTION`, { | ||
name: `LANGUAGE`, | ||
valueString: `ts`, | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe(`skip and ts flag`, () => { | ||
beforeEach(() => { | ||
parseArgsMock.mockReturnValueOnce({ | ||
flags: { yes: true, ts: true }, | ||
dirName, | ||
}) | ||
}) | ||
|
||
it(`should use the TS starter`, async () => { | ||
await run() | ||
expect(initStarter).toHaveBeenCalledWith( | ||
DEFAULT_STARTERS.ts, | ||
dirName, | ||
[], | ||
dirName | ||
) | ||
}) | ||
it(`should track TS was selected as language`, async () => { | ||
await run() | ||
expect(trackCli).toHaveBeenCalledWith(`CREATE_GATSBY_SELECT_OPTION`, { | ||
name: `LANGUAGE`, | ||
valueString: `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,52 @@ | ||
import { parseArgs } from "../../utils/parse-args" | ||
import { reporter } from "../../utils/reporter" | ||
|
||
const dirNameArg = `hello-world` | ||
|
||
jest.mock(`../../utils/reporter`) | ||
|
||
describe(`parseArgs`, () => { | ||
it(`should parse without flags and dir name`, () => { | ||
const { flags, dirName } = parseArgs([]) | ||
expect(flags.yes).toBeFalsy() | ||
expect(flags.ts).toBeFalsy() | ||
expect(dirName).toEqual(``) | ||
}) | ||
it(`should parse with dir name without flags`, () => { | ||
const { flags, dirName } = parseArgs([dirNameArg]) | ||
expect(flags.yes).toBeFalsy() | ||
expect(flags.ts).toBeFalsy() | ||
expect(dirName).toEqual(dirNameArg) | ||
}) | ||
it(`should parse with flags before dir name`, () => { | ||
const { flags, dirName } = parseArgs([`-y`, `-ts`, dirNameArg]) | ||
expect(flags.yes).toBeTruthy() | ||
expect(flags.ts).toBeTruthy() | ||
expect(dirName).toEqual(dirNameArg) | ||
}) | ||
it(`should parse with flags after dir name`, () => { | ||
const { flags, dirName } = parseArgs([dirNameArg, `-y`, `-ts`]) | ||
expect(flags.yes).toBeTruthy() | ||
expect(flags.ts).toBeTruthy() | ||
expect(dirName).toEqual(dirNameArg) | ||
}) | ||
it(`should parse with flags before and after dir name`, () => { | ||
const { flags, dirName } = parseArgs([`-y`, dirNameArg, `-ts`]) | ||
expect(flags.yes).toBeTruthy() | ||
expect(flags.ts).toBeTruthy() | ||
expect(dirName).toEqual(dirNameArg) | ||
}) | ||
it(`should warn if unknown flags are used`, () => { | ||
const unknownFlag = `-unknown` | ||
const { flags, dirName } = parseArgs([dirNameArg, unknownFlag]) | ||
expect(reporter.warn).toBeCalledTimes(1) | ||
expect(reporter.warn).toBeCalledWith( | ||
expect.stringContaining( | ||
`Found unknown argument "${unknownFlag}", ignoring. Known arguments are: -y, -ts` | ||
) | ||
) | ||
expect(flags.yes).toBeFalsy() | ||
expect(flags.ts).toBeFalsy() | ||
expect(dirName).toEqual(dirNameArg) | ||
}) | ||
}) |
Oops, something went wrong.