Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to run tests from project directory? #32

Open
Niakr1s opened this issue Mar 27, 2024 · 4 comments
Open

Is there a way to run tests from project directory? #32

Niakr1s opened this issue Mar 27, 2024 · 4 comments

Comments

@Niakr1s
Copy link

Niakr1s commented Mar 27, 2024

Hello! Thanks for your extension!

I have a problem. I have test data in '{project_dir}/testdata' folder, and I want them to load it from filesystem in tests. But it seems that Test Explorer runs tests with cwd set to test file' directory.

For example, if I try to do this in file 'tests/foo/bar.test.js':

let raw = readFileSync('./testdata/raw.json', 'utf-8');

I'm getting cryptic window with message: 'The editor could not be opened because the file was not found.' Command 'node --test' from terminal, if I run it from project directory, works great though.

However, if I change filepath to relative:

let raw = readFileSync('../../testdata/raw.json', 'utf-8');

it works smoothly.

I made a small investigation: command process.cwd(), executed in a debug console, gets me directory of a test file. In comparison, same command when I run tests with jest, results in the project directory.

So, is it possible to fix it so that tests always run from project directory?

@Niakr1s
Copy link
Author

Niakr1s commented Mar 27, 2024

Upd: I ended up in using pkg-dir module, it works good.

@connor4312
Copy link
Owner

connor4312 commented Mar 27, 2024

Yea, I agree that tweaking this makes sense. I will probably have some "projectRoot" setting, defaulting to your workspace folder, that controls the cwd among other things (things that we naively root at the workspace folder today)

@Niakr1s
Copy link
Author

Niakr1s commented Mar 27, 2024

That would've been great!

@techfg
Copy link

techfg commented Apr 12, 2024

Great extension, thank you for your work!

Running in to this same issue - tests are all written assuming execution from projectRoot and fail when using test runner. Current workaround is:

settings.json

{
    "nodejs-testing.pretest": [
        "./test-prep.sh"
    ]
}

test-prep.sh

cd ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants