Skip to content

Commit 22122c4

Browse files
committed
fix: Sass partials imports from node_modules
Sass supports importing partials with or without the leading underscore: https://sass-lang.com/documentation/at-rules/import#partials Because `applyModuleNameMapper()` uses `require.resolve()` to employ node module resolution on a bare import, it throws an exception if the import elides the leading `_` from the basename.
1 parent d207d06 commit 22122c4

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

e2e/__projects__/style/components/Scss.vue

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
<style lang="scss" module>
66
@import '~__styles/scss-a';
7+
@import '~vue-jest-test/partial.scss';
78
89
.c {
910
background-color: $primary-color;

e2e/__projects__/style/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"private": true,
77
"scripts": {
8-
"test": "jest --no-cache test.js"
8+
"test": "node setup.js && jest --no-cache test.js"
99
},
1010
"dependencies": {
1111
"vue": "^2.5.21",
@@ -17,8 +17,7 @@
1717
"@vue/test-utils": "^1.0.0-beta.28",
1818
"jest": "^24.0.0",
1919
"postcss": "^7.0.13",
20-
"sass": "^1.23.7",
21-
"vue-jest": "file:../../../"
20+
"sass": "^1.23.7"
2221
},
2322
"jest": {
2423
"moduleFileExtensions": [

e2e/__projects__/style/setup.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs')
2+
3+
const testDir = 'node_modules/vue-jest-test'
4+
5+
if (!fs.existsSync(testDir)) {
6+
fs.mkdirSync(testDir)
7+
}
8+
9+
fs.openSync(`${testDir}/_partial.scss`, 'w')

e2e/test-runner.js

+34-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const fs = require('fs-extra')
44
const chalk = require('chalk')
55

66
const IGNORE_FILES = ['.DS_Store']
7+
const cwd = process.cwd()
8+
9+
// Can be run as `yarn test:e2e --cache` to forego reinstalling node_modules, or
10+
// `yarn test:e2e <projects dir>`, or `yarn test:e2e --cache <projects dir>`.
11+
const args = process.argv.slice(2)
712

813
function success(msg) {
914
console.info(chalk.green('\n[vue-jest]: ' + msg + '\n'))
@@ -31,29 +36,49 @@ function runTest(dir) {
3136

3237
const log = msg => info(`(${dir}) ${msg}`)
3338

34-
log('Running tests')
39+
if (!args.filter(arg => arg === '--cache').length) {
40+
log('Removing node_modules')
41+
fs.removeSync(`${resolvedPath}/node_modules`)
3542

36-
log('Removing node_modules')
37-
fs.removeSync(`${resolvedPath}/node_modules`)
43+
log('Removing package-lock.json')
44+
fs.removeSync(`${resolvedPath}/package-lock.json`)
3845

39-
log('Removing package-lock.json')
40-
fs.removeSync(`${resolvedPath}/package-lock.json`)
46+
log('Installing node_modules')
47+
run('npm install --silent')
48+
}
4149

42-
log('Installing node_modules')
43-
run('npm install --silent')
50+
// For tests that need vue-jest to successfully `require.resolve()` a file in
51+
// the project directory's node_modules, we can't symlink vue-jest from a
52+
// parent directory (as node module resolution walks up the file tree,
53+
// starting from the realpath of the caller), we must copy it.
54+
if (
55+
!fs.existsSync(`${resolvedPath}/node_modules/vue-jest`) ||
56+
!fs.lstatSync(`${resolvedPath}/node_modules/vue-jest`).isSymbolicLink()
57+
) {
58+
log('Copying vue-jest into node_modules')
59+
fs.mkdirSync(`${resolvedPath}/node_modules/vue-jest`, { recursive: true })
60+
run(`cp ${cwd}/package.json node_modules/vue-jest/`)
61+
run(`cp -r ${cwd}/lib node_modules/vue-jest/`)
62+
}
4463

4564
log('Running tests')
4665
run('npm run test')
4766

4867
success(`(${dir}) Complete`)
4968
}
5069

51-
async function testRunner(dir) {
70+
async function testRunner() {
5271
const directories = fs
5372
.readdirSync(path.resolve(__dirname, '__projects__'))
5473
.filter(d => !IGNORE_FILES.includes(d))
5574

56-
directories.forEach(runTest)
75+
const matches = args.filter(d => directories.includes(d))
76+
77+
if (matches.length) {
78+
matches.forEach(runTest)
79+
} else {
80+
directories.forEach(runTest)
81+
}
5782
}
5883

5984
testRunner()

lib/module-name-mapper-helper.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ const matchModuleImport = /^[^?]*~/
1111
function resolve(to, importPath, fileType) {
1212
importPath =
1313
path.extname(importPath) === '' ? `${importPath}.${fileType}` : importPath
14+
1415
if (path.isAbsolute(importPath)) {
1516
return importPath
1617
} else if (matchModuleImport.test(importPath)) {
17-
return require.resolve(importPath.replace(matchModuleImport, ''))
18+
const dirname = path.dirname(importPath).replace(matchModuleImport, '')
19+
const basename = path.basename(importPath)
20+
21+
try {
22+
return require.resolve(path.join(dirname, basename))
23+
} catch (_) {
24+
return require.resolve(path.join(dirname, `_${basename}`))
25+
}
1826
}
1927
return path.join(path.dirname(to), importPath)
2028
}

0 commit comments

Comments
 (0)