-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby-dev-cli): infer correct prefix from package path #8683
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ed4175d
fix: infer correct prefix from package path
DSchau 109dedb
test(gatsby-dev-cli): add tests to gatsby-dev-cli
DSchau ee8992e
test: attempt to get tests passing in CI
DSchau fa07785
ci: hopefully get tests passing
DSchau d0397d1
test: get final test passing
DSchau 36ebe2a
scripts: remove quiet flag for more helpful netlify debugging
DSchau 2757ca9
fix(gatsby-dev-cli): ensure non-existant files don't break cli
DSchau a09109c
chore: Add a comment so we're tracking on the issue
DSchau 6e3076b
chore: also make sure directories are unique
DSchau 6d07894
test: add test for duplicate directories
DSchau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,140 @@ | ||
jest.mock(`chokidar`, () => { | ||
return { | ||
watch: jest.fn(), | ||
} | ||
}) | ||
jest.mock(`fs-extra`, () => { | ||
return { | ||
copy: jest.fn(), | ||
existsSync: jest.fn(), | ||
} | ||
}) | ||
const chokidar = require(`chokidar`) | ||
const fs = require(`fs-extra`) | ||
const path = require(`path`) | ||
const watch = require(`../watch`) | ||
|
||
let on | ||
beforeEach(() => { | ||
fs.copy.mockReset() | ||
fs.existsSync.mockImplementation(() => true) | ||
chokidar.watch.mockImplementation(() => { | ||
const mock = { | ||
on: jest.fn().mockImplementation(() => mock), | ||
} | ||
on = mock.on | ||
return mock | ||
}) | ||
}) | ||
|
||
describe(`watching`, () => { | ||
const callEventCallback = (...args) => | ||
on.mock.calls[0].slice(-1).pop()(...args) | ||
const callReadyCallback = (...args) => | ||
on.mock.calls[1].slice(-1).pop()(...args) | ||
|
||
const args = [process.cwd(), [`gatsby`], {}] | ||
|
||
it(`watches files`, () => { | ||
watch(...args) | ||
expect(chokidar.watch).toHaveBeenCalledTimes(1) | ||
expect(chokidar.watch).toHaveBeenCalledWith(expect.any(Array), { | ||
ignored: [expect.any(Function)], | ||
}) | ||
}) | ||
|
||
it(`registers on handlers`, () => { | ||
watch(...args) | ||
|
||
expect(on).toHaveBeenCalledTimes(2) | ||
expect(on).toHaveBeenLastCalledWith(`ready`, expect.any(Function)) | ||
}) | ||
|
||
describe(`copying files`, () => { | ||
it(`does not copy files on non-watch event`, () => { | ||
watch(...args) | ||
|
||
callEventCallback(`test`) | ||
|
||
expect(fs.copy).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`copies files on watch event`, () => { | ||
const filePath = path.join(process.cwd(), `packages/gatsby/dist/index.js`) | ||
watch(...args) | ||
callEventCallback(`add`, filePath) | ||
|
||
expect(fs.copy).toHaveBeenCalledTimes(1) | ||
expect(fs.copy).toHaveBeenCalledWith( | ||
filePath, | ||
`node_modules/gatsby/dist/index.js`, | ||
expect.any(Function) | ||
) | ||
}) | ||
|
||
it(`copies cache-dir files`, () => { | ||
watch(...args) | ||
|
||
const filePath = path.join( | ||
process.cwd(), | ||
`packages/gatsby/cache-dir/register-service-worker.js` | ||
) | ||
callEventCallback(`add`, filePath) | ||
|
||
expect(fs.copy).toHaveBeenCalledTimes(2) | ||
expect(fs.copy).toHaveBeenLastCalledWith( | ||
filePath, | ||
`.cache/register-service-worker.js`, | ||
expect.any(Function) | ||
) | ||
}) | ||
|
||
it(`filters non-existant files/directories`, () => { | ||
fs.existsSync.mockReset().mockImplementation(file => false) | ||
|
||
watch(...args) | ||
|
||
expect(chokidar.watch).toHaveBeenCalledWith([], expect.any(Object)) | ||
}) | ||
|
||
it(`filters duplicate directories`, () => { | ||
watch(process.cwd(), [`gatsby`, `gatsby`], {}) | ||
|
||
expect(chokidar.watch).toHaveBeenCalledWith( | ||
[expect.stringContaining(`gatsby`)], | ||
expect.any(Object) | ||
) | ||
}) | ||
}) | ||
|
||
describe(`exiting`, () => { | ||
let realProcess | ||
beforeAll(() => { | ||
realProcess = global.process | ||
|
||
global.process = { | ||
...realProcess, | ||
exit: jest.fn(), | ||
} | ||
}) | ||
|
||
afterAll(() => { | ||
global.process = realProcess | ||
}) | ||
|
||
it(`does not exit if scanOnce is not defined`, () => { | ||
watch(...args) | ||
callReadyCallback() | ||
|
||
expect(process.exit).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`exits if scanOnce is defined`, async () => { | ||
watch(process.cwd(), [`gatsby`], { scanOnce: true }) | ||
|
||
await callReadyCallback() | ||
|
||
expect(process.exit).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given something like
packages/gatsby/dist/gatsby/cache-dir/something.js
this was inferring thepackageName
to becache-dir
rather than gatsby. This is the main fix here.