Skip to content

Use test fixtures #34

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

Merged
merged 2 commits into from
Nov 13, 2020
Merged

Use test fixtures #34

merged 2 commits into from
Nov 13, 2020

Conversation

ehmicky
Copy link

@ehmicky ehmicky commented Nov 13, 2020

I am hitting two issues with mock-fs: here and there.

When I try to add more tests, I get:

RUNS  test/index.js
node:fs:1732
      handleErrorFromBinding(ctx);
      ^

Error: ENOENT: no such file or directory, lstat '/home/ether/netlify/netlify-plugin-nextjs/node_modules/jest/node_modules/chalk'
    at Object.realpathSync (node:fs:1732:7)
    at toRealPath (node:internal/modules/cjs/loader:368:13)
    at tryFile (node:internal/modules/cjs/loader:364:10)
    at tryExtensions (node:internal/modules/cjs/loader:376:22)
    at tryPackage (node:internal/modules/cjs/loader:322:5)
    at Function.Module._findPath (node:internal/modules/cjs/loader:540:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:887:27)
    at Function.Module._load (node:internal/modules/cjs/loader:745:27)
    at Module.require (node:internal/modules/cjs/loader:972:19)
    at require (node:internal/modules/cjs/helpers:88:18) {
  syscall: 'lstat',
  code: 'ENOENT',
  errno: -2,
  path: '/home/ether/netlify/netlify-plugin-nextjs/node_modules/jest/node_modules/chalk'
}

Apparently, the issue is the following: after filesystem has been mocked, jest lazily try to require('chalk') by using fs instead. But because mocking is in place, this crashes.

This new lazy loading behavior was added to mock-fs@4. I tried downgrading to mock-fs@3 but an error message shows me it won't run on Node 15.

This PR implements an alternative: using a fixtures directory with static files committed to git, then do process.chdir() inside tests that need filesystem access.

@ehmicky ehmicky added the type: chore work needed to keep the product and development running smoothly label Nov 13, 2020
@ehmicky ehmicky self-assigned this Nov 13, 2020
Copy link

@lindsaylevine lindsaylevine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa this is clever, very awesome !!!!!!!

thought: i was originally using appendFile to create the next config if it doesn't exist in the project so in this test 'create next.config.js with correct target if file does not exist' i first tried using mock-fs or trying to mock appendFile and it was such a pain that i switched to make-file. would this fixture approach work for mocking appendFile and if so, how?

@ehmicky
Copy link
Author

ehmicky commented Nov 13, 2020

Good point!
I have added a new commit to use a temporary directory in each test instead, so that mocking is not necessary.
The less we mock, the closer we are to the production code, which will help increase our confidence in the tests.
Please let me know what you think!

Copy link

@lindsaylevine lindsaylevine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is so awesome, i def have a lot more to learn and a lot more experience to gain with node 🙏 thank you for doing this!!!!!!!

utils.build.failBuild.mockReset()
utils.run.command.mockReset()
jest.clearAllMocks()
jest.resetAllMocks()

this.restoreCwd()
await this.cleanup()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does cleanup do and why is it async?

Copy link
Author

@ehmicky ehmicky Nov 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some code comment 👍

@ehmicky ehmicky force-pushed the chore/test-fixtures branch from b1a9e64 to b31cf24 Compare November 13, 2020 20:35
@ehmicky ehmicky merged commit ea07d97 into main Nov 13, 2020
@ehmicky ehmicky deleted the chore/test-fixtures branch November 13, 2020 20:38
@ehmicky
Copy link
Author

ehmicky commented Nov 13, 2020

Quick run-through of the setup:

Then, some tests might need to use a specific fixture directory instead of an empty temporary directory. To do this:

serhalp pushed a commit that referenced this pull request Apr 5, 2024
* chore: big reorg

* chore: simplify content handling

* chore: add comments

* feat: another refactor

* fix: content filtering logic

* chore: promise all style

* chore: update eslint override path

* chore: update readme

* fix: update missed server content paths

* chore: update FetchCacheValue type

* chore: make content type logic more explicit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: chore work needed to keep the product and development running smoothly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants