jest-css-modules-transform icon indicating copy to clipboard operation
jest-css-modules-transform copied to clipboard

SassError: Can't find stylesheet to import.

Open krutoo opened this issue 4 years ago • 11 comments

Hi, i use jest-css-modules-transform with typescript files and in my .scss files i have something like this:

@use '~foo/bar/baz.scss';

// ....some scss here

And in mye tests i have error:

SassError: Can't find stylesheet to import.

Looks like in jest environment sass cannot resolve tilde

how can i solve this problem?

krutoo avatar Feb 10 '21 07:02 krutoo

@krutoo can you provide your sass and jest config? And also webpack config. Maybe it works with Webpack because you have some custom import rules.

Connormiha avatar Feb 11 '21 21:02 Connormiha

there is no sass config in this project

jest config:

module.exports = {
  preset: 'ts-jest/presets/js-with-babel',
  setupFiles: ['./jest/setup.js', 'jest-canvas-mock'],
  globalSetup: '<rootDir>/jest/test-env.js',
  globals: {
    'ts-jest': {
      tsConfig: '<rootDir>/tsconfig.jest.json',
    },
  },
  transformIgnorePatterns: [
    '/node_modules/(?!(@FOO/|@foo-bar/|middleware-axios)).*/',
  ],
  transform: {
    '\\.svg$': '<rootDir>/jest/transforms/svg.js',
    '\\.(css|scss)$': 'jest-css-modules-transform',
    '\\.(jpg|jpeg|png|gif|eot|otf|ttf|woff|woff2)$': '<rootDir>/jest/transforms/media.js',
  },
  testPathIgnorePatterns: [
    '<rootDir>/.yarn-cache/',
    '<rootDir>/node_modules/',
  ],
  modulePathIgnorePatterns: [
    '<rootDir>/.yarn-cache/',
    '<rootDir>/build/',
  ],
  coveragePathIgnorePatterns: [
    '\\.scss$',
    '\\.svg$',
    '/jest/',
    '/common/cqc/',
  ],
  coverageThreshold: {
    global: {
      branches: 100,
      functions: 100,
      lines: 100,
      statements: 100,
    },
  },
  clearMocks: true,
};

krutoo avatar Feb 12 '21 13:02 krutoo

@Connormiha it looks like I thought it was built-in functionality of sass/jest-css-modules-transform

Do you have any idea how you can add support for this?

This code works:

@use 'node-modules/@foo-bar/baz.scss';

krutoo avatar Feb 12 '21 13:02 krutoo

You can use custom importer for sass config https://github.com/Connormiha/jest-css-modules-transform#options https://sass-lang.com/documentation/js-api#importer

Connormiha avatar Feb 28 '21 11:02 Connormiha

@Connormiha Thanks

krutoo avatar Mar 01 '21 10:03 krutoo

I had the same issue, fixed through customizing the sass importer a bit

function importer(url, prev, done) {

    // if the url (file I want to import) match, resolve from node modules
    if (url === 'lesli-css') {
        url = path.resolve('node_modules', url);
    }

    return { file: url };
}

sassTestFiles.forEach(file => sassTrue.runSass({ importer, file }, { describe, it }))

ldonis avatar Apr 25 '22 03:04 ldonis

@Connormiha, sorry for reopen but I have same issue but now with:

@use 'pkg:@sima-land/ui-nucleons/colors';

The pkg: prefix is new way to use packages, should it works by default in jest-css-modules-transform?

Details: https://sass-lang.com/documentation/at-rules/use/#pkg-ur-ls

If no, how can i enable this?


I tried this but it is not working:

// jest-css-modules-transform-config.js
const sass = require('sass');

module.exports = {
  sassConfig: {
    importers: [new sass.NodePackageImporter()],
  },
};

krutoo avatar Jun 21 '24 12:06 krutoo

@krutoo Hello. It should work. It just pass config to sass module. Could you please show error message and ensure path of jest-css-modules-transform-config.js?

Connormiha avatar Jun 21 '24 21:06 Connormiha

@Connormiha I noticed that jest-css-modules-transform uses sass.renderSync which is deprecated according to Sass docs: https://sass-lang.com/documentation/js-api/functions/rendersync/

Looks like it is accepts importer (deprecated) but not importers (new) option.

Also, looks like importer does not support NodePackageImporter


Could you please show error message...

It is same error "SassError: Can't find stylesheet to import." with @use 'pkg:...

krutoo avatar Jun 22 '24 05:06 krutoo

@Connormiha I made a quick alternative to suit my needs, you can see what I came up with: https://github.com/krutoo/jest-css-modules-transform/blob/main/src/index.ts

krutoo avatar Jun 22 '24 05:06 krutoo

@krutoo Now test work sync. I will try to fix it soon, but it is a big changes. Thanks.

Connormiha avatar Jul 01 '24 07:07 Connormiha