Skip to content
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

__PATH_PREFIX__ for testing with enzyme/jest #13115

Closed
daniellizik opened this issue Apr 4, 2019 · 2 comments
Closed

__PATH_PREFIX__ for testing with enzyme/jest #13115

daniellizik opened this issue Apr 4, 2019 · 2 comments

Comments

@daniellizik
Copy link

daniellizik commented Apr 4, 2019

Related to #1378

I'm still seeing this in a enzyme/jest test:

gatsby-config.js

module.exports = {
  // this doesn't affect the test at all, it seems
  pathPrefix: ' '
};

test

import { render } from 'enzyme';
import { Link } from 'gatsby';

it('renders', () => {
  const wrapper = render(<Link to="/foo" />);
  // ReferenceError: __PATH_PREFIX__ is not defined
});

// this one is fine
it('provides the expected global', () => {
  global.__PATH_PREFIX__ = 'test';
  const wrapper = render(<Link to="/foo" />);
});

The docs on pathPrefix don't explicitly mention what to do during testing

During development, write paths as if there was no path prefix e.g. for a blog hosted at example.com/blog, don’t add /blog to your links. The prefix will be added when you build for deployment.

Maybe here it would be better to do something like

export function withPrefix(path) {
  const pathPrefix = typeof __PATH_PREFIX__ !== 'undefined' ? __PATH_PREFIX__ : '';
  return normalizePath(`${pathPrefix}/${path}`)
}

Probably a better solution would be to inject the global if NODE_ENV is test?

@gatsbot
Copy link

gatsbot bot commented Apr 4, 2019

Fails
🚫

Danger failed to run /app/danger-0.mxj5fujn0ls.ts,/app/danger-0.1ueluu1v0to.ts.

Error Error

ENOENT: no such file or directory, open '/app/danger-0.1ueluu1v0to.ts'

Error: ENOENT: no such file or directory, open '/app/danger-0.1ueluu1v0to.ts'
    at Object.openSync (fs.js:439:3)
    at Object.readFileSync (fs.js:344:35)
    at Object.<anonymous> (/app/node_modules/danger/distribution/runner/runners/vm2.js:106:87)
    at step (/app/node_modules/danger/distribution/runner/runners/vm2.js:43:23)
    at Object.next (/app/node_modules/danger/distribution/runner/runners/vm2.js:24:53)
    at fulfilled (/app/node_modules/danger/distribution/runner/runners/vm2.js:15:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Dangerfile

import { danger, markdown } from "danger"

const getMessage = (username: string) => `\
@${username} We noticed that the body of this issue is blank.

Please fill in this field with more information to help the \
maintainers resolve your issue.\
`

export const emptybody = () => {
  const {
    user: { login: username },
    body,
  } = danger.github.issue as any

  if (body.trim().length === 0) {
    markdown(getMessage(username))
  }
}

export default async () => {
  emptybody()
}

---

Generated by 🚫 dangerJS

@daniellizik
Copy link
Author

sorry found this https://www.gatsbyjs.org/docs/unit-testing/#2-creating-a-configuration-file-for-jest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant