question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

imports not working when done through tests

See original GitHub issue

Reproduce

Install fresh RW 0.14 yarn rw g page post yarn rw g layout post

Modify PostPage to include PostLayout, and wrap everything in the layout

import { Link } from '@redwoodjs/router'
import PostLayout from 'src/layouts/PostLayout'

const PostPage = () => {
  return (
    <PostLayout>
      <h1>PostPage</h1>
      <p>Find me in "./web/src/pages/PostPage/PostPage.js"</p>
      <p>
        My default route is named "post", link to me with `
        <Link to="post">routes.post()</Link>`
      </p>
    </PostLayout>
  )
}

export default PostPage

Run all tests with yarn rw test

Expected

All tests pass

Actual

 FAIL   web  web/src/pages/PostPage/PostPage.test.js
  ● Test suite failed to run

    Cannot find module '../../../../src/layouts/PostLayout' from 'src/pages/PostPage/PostPage.js'

    Require stack:
      src/pages/PostPage/PostPage.js
      src/pages/PostPage/PostPage.test.js

      1 | import { Link } from '@redwoodjs/router'
    > 2 | import PostLayout from 'src/layouts/PostLayout'
        | ^
      3 |
      4 | const PostPage = () => {
      5 |   return (

      at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:308:11)
      at Object.<anonymous> (src/pages/PostPage/PostPage.js:2:1)

 PASS   web  web/src/layouts/PostLayout/PostLayout.test.js (11.293 s)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        13.451 s
Ran all test suites.

Note

I need #869 first, before I can actually get this far.

Actually running the app with yarn rw dev works fine. No problems with the imports

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Tobbecommented, Jul 27, 2020

Didn’t have anything up on Github, so I just now created a new project and pushed that https://github.com/Tobbe/v14 <TestPage> has a relative import of <TestLayout> that doesn’t work when running yarn rw test

1reaction
peterpcommented, Jul 22, 2020

It looks like jest is incorrectly determining the src alias from babel-plugin-module-resolver; I think we might need to do something like this: https://www.robinwieruch.de/babel-module-resolver-jest

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing Python; Imports breaking - Stack Overflow
Running the test I find I have problems with the imports in the main class. To resolve I have to change the imports...
Read more >
[BUG] Unable to import module in test [ERR_REQUIRE_ESM]
A test with some imports and global setup. The doc won't help because it uses global setup, imports doesn't work due to the...
Read more >
Good Integration Practices — pytest documentation
In prepend and append import-modes, if pytest finds a "a/b/test_module.py" test file while recursing into the filesystem it determines the import name as ......
Read more >
Problems regarding test and module imports - Julia Discourse
Hello, I'm having some trouble structuring my project so that i can easily run tests. Right now I have the following structure: Project/...
Read more >
Traps for the Unwary in Python's Import System
I'm cross-referencing packages that all reside under 'project' and I hit a problem with an internal import that's using explicit relative imports (i.e....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found