avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Setting CWD to directory of test #2591

Closed jakobrosenberg closed 4 years ago

jakobrosenberg commented 4 years ago

This isn't as much a bug as a shortcoming of documentation.

Searching for CWD shows countless commits and issues, but all documentation entries have been removed and it's unclear if it's possible to set CWD to the directories of the respective tests.

novemberborn commented 4 years ago

It's documented in the third paragraph here: https://github.com/avajs/ava/blob/master/docs/01-writing-tests.md:

AVA tries to run test files with their current working directory set to the directory that contains your package.json file.

Perhaps we should add (CWD) after "current working directory"? I did search for "working directory" rather than CWD.


You should be able to change the working directory, say in a script you require(). You can use AVA's meta export to determine the path to the current test file.

jakobrosenberg commented 4 years ago

Thanks @novemberborn. I saw the paragraph you mentioned, but it doesn't mention if the CWD is merely a default or hardcoded. Looking through the issues and commits it's easy to get the impression that this can be configured.

Do you have an example of how I would change the CWD?

novemberborn commented 4 years ago

It's not configurable in AVA, but Node.js has APIs for this. You could add a helper like _cwd.js:

const path = require('path')
const test = require('ava')

process.chdir(path.dirname(test.meta.file))

And then in AVA's config add require: ['_cwd.js'].

(Note that I haven't tested this.)

jakobrosenberg commented 4 years ago

Much appreciated! I'll close this issue. 🙂