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.

Imported constants in styles no longer work with Jest in Next 6.0.0

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Importing a constant and using it in a <style jsx> tag should work as per the documentation in tests using Jest

Current Behavior

The imported constant is undefined when used in the <style jsx> template string (but is still defined elsewhere in the file)

Steps to Reproduce (for bugs)

Make the following minor changes to the with-jest example app (https://github.com/zeit/next.js/tree/canary/examples/with-jest):

// with-jest-app/pages/index.js
import { red } from '../colors'; // new

export default () => (
  <div>
    <style jsx>{`
      p {
        color: ${red};
      }
    `}</style>
    <p>Hello World!</p>
  </div>
);
// new file: with-jest-app/colors.js
export const red = '#F00';

No other changes to config, etc are required to reproduce the issue.

In Next 5.x this works as expected both when running the app and also when using Jest. In Next 6.0.0 this works as expected when running the app but the Jest tests fail claiming: ReferenceError: red is not defined.

I’ve found these workarounds that solve the issue (albeit in an ugly way) and may help you to diagnose the issue:

  1. Assign to another variable first:
// with-jest-app/pages/index.js
import { red } from '../colors';

const alias = red;

export default () => (
  <div>
    <style jsx>{`
      p {
        color: ${alias};
      }
    `}</style>
    <p>Hello World!</p>
  </div>
);
  1. Convert the variable expression into a boolean expression:
// with-jest-app/pages/index.js
import { red } from '../colors';

export default () => (
  <div>
    <style jsx>{`
      p {
        color: ${true && red};
      }
    `}</style>
    <p>Hello World!</p>
  </div>
);

Context

Was trying to upgrade another (much larger) app to Next v6.0.0 Note: It’s entirely possible that this is actually an issue with another project (styled-jsx, jest, babel, etc) but as I noticed it when upgrading Next and am not sure how to narrow it down any further I have raised the issue here - I’m happy to close this issue and raise it on one of the other projects if you think that’s more appropriate.

Your Environment

Tech Version
next 6.0.0
node 8.6.0
OS Linux
Jest 22.4.3

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jjm340commented, May 13, 2018

I found another workaround: `import * as colors from ‘…/colors’;

export default () => (

<div> <style jsx>{` p { color: ${colors.red}; } `}</style>

Hello World!

</div> ); `

This worked for me

0reactions
timneutkenscommented, Mar 9, 2019

I guess this was fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Imported constants in styles no longer work with Jest in Next ...
In Next 6.0.0 this works as expected when running the app but the Jest tests fail claiming: ReferenceError: red is not defined ....
Read more >
Manual mock with jest isn't working for imported constants
Suppose that I want to test the status method shown here in src/age.ts by mocking the CURRENT_AGE variable. // src/age.ts import { CURRENT_AGE...
Read more >
Changelog - Cypress Documentation
Imports in component testing support files are no longer tree-shaken by Webpack. Fixes #24117. cy.session() commands will correctly fail when the setup function...
Read more >
Node.js v19.3.0 Documentation
If the function has not been called exactly exact times when tracker.verify() is called, then tracker.verify() will throw an error. import assert from...
Read more >
Releases - styled-components
Fix React Native components accepts function as style prop. ... We no longer emit dynamic classNames for empty rulesets, so some className churn...
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