I don't want to use "../" in JavaScript test code import path specification

Asked 2 years ago, Updated 2 years ago, 18 views

Currently, I am thinking of writing a test using a combination of gulp+babel+mocha.
The production code is located under the root/src/modules directory.

I would like to place the test code under root/test/modules in the same configuration as the production code.

In this case, the test code contains

import shut from '../../../(repeatUntilRoot...)/src/mosules/(pathToSUT...)'

It is very complicated to write

Webpack is used to build production code

import someModule from 'modules/path/to/module'

It can be resolved as shown in .
Would it be possible to call the module under test from the test code using the same description as the above production code without using Webpack during the test?

javascript

2022-09-29 21:55

2 Answers

It is possible by setting it as an environment variable. exportNODE_PATH=/path/to/project/root/src


2022-09-29 21:55

How about using wavy?The package.json location is treated as through.

 mkdir foo & cd foo
echo'module.exports="foo"'>index.js

npm install wavy
node-e "console.log(require('~/index.js')')"
# foo


2022-09-29 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.