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

jest: add 'src' path to jest default config #2039

Closed
jangxyz opened this issue Mar 19, 2021 · 2 comments
Closed

jest: add 'src' path to jest default config #2039

jangxyz opened this issue Mar 19, 2021 · 2 comments
Labels
bug/needs-info More information is needed for reproduction topic/testing

Comments

@jangxyz
Copy link
Contributor

jangxyz commented Mar 19, 2021

Importing from absolute paths in test files throws "Cannot find module" error.

Currently this test code:

import { render } from '@redwoodjs/testing'

import HomePage from './HomePage'
import data from 'src/model/data' // <-- here

describe('HomePage', () => {
  it('renders successfully', () => {
    expect(() => {
      render(<HomePage />)
    }).not.toThrow()
  })

  it('reads data', () => {
    expect(data).toBe(3)
  })
})

throws error:

image

Changing it to relative path (import data from '../../model/data') works fine.

As redwood supports src path alias out of the box, it seems natural to expect the same for test codes as well.

I got it working by adding moduleNameMapper configuration to web/config/jest.config.js:
(from jest configuration#moduleNameMapper)

config.moduleNameMapper = config.moduleNameMapper || {}
config.moduleNameMapper['src/(.*)'] = '<rootDir>/src/$1'

Since like jest.createConfig.ts is already using moduleNameMapper heavily,

moduleNameMapper: {
/**
* Make sure modules that require different versions of these
* dependencies end up using the same one.
*/
'^react$': path.join(NODE_MODULES_PATH, 'react'),
'^react-dom$': path.join(NODE_MODULES_PATH, 'react-dom'),
'^@apollo/client/react$': path.join(
NODE_MODULES_PATH,
'@apollo/client/react'
),
// We replace imports to "@redwoodjs/router" with our own implementation.
'^@redwoodjs/router$': path.join(
NODE_MODULES_PATH,
'@redwoodjs/testing/dist/MockRouter.js'
),
'^@redwoodjs/web$': path.join(NODE_MODULES_PATH, '@redwoodjs/web'),
'^@redwoodjs/testing$': path.join(
NODE_MODULES_PATH,
'@redwoodjs/testing'
),
'~__REDWOOD__USER_ROUTES_FOR_MOCK': getPaths().web.routes,
/**
* Mock out files that aren't particularly useful in tests. See fileMock.js for more info.
*/
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css)$':
'@redwoodjs/testing/dist/fileMock.js',
},

I think it would be plausible to add src alias too.

@peterp
Copy link
Contributor

peterp commented Mar 19, 2021

Weird, this should be handled by babel - which jest should be using

@peterp peterp added the bug/needs-info More information is needed for reproduction label Mar 21, 2021
@jtoar
Copy link
Contributor

jtoar commented Dec 10, 2021

@dac09 fixed this in a recent release (perhaps the last one even):

image

image

@jtoar jtoar closed this as completed Dec 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/needs-info More information is needed for reproduction topic/testing
Projects
None yet
Development

No branches or pull requests

3 participants