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

Problems with Relative Paths and Tests #175

Closed
bookercodes opened this issue Nov 9, 2015 · 12 comments
Closed

Problems with Relative Paths and Tests #175

bookercodes opened this issue Nov 9, 2015 · 12 comments

Comments

@bookercodes
Copy link
Contributor

I am using the config module to load configuration files in my Node app.

config looks for a dir called ./config relative to the current working dir.

When running tests via AVA, the current working directory is the location of the tests. This causes config to look in the wrong place for the ./config dir. (I think this is due to the way AVA spawns processes. )

To work around this I use an environment variable recognized by config to change the ./config dir location when running tests:

NODE_ENV=test NODE_CONFIG_DIR=../../config ava test/**/*.js

This is pretty teampramental, though - some test files are nested differently to others. What's more, some modules look for files relative to process.cwd() by default. In some cases, I can configure that but I think it is going to be very tedious to do.

How should I work around this if I want to use AVA?

@vadimdemedes
Copy link
Contributor

Could you please post a few lines of your code?

Related: #32

@MarkHerhold
Copy link

I hit the exact same issue with AVA and the config module. The working dir needs to be the directory where I launch test my process, not where the test file lives.

@sindresorhus
Copy link
Member

@MarkHerhold Read #32 for context of why it's like it is. I'm surprised the config module doesn't just find the upwards closest config file.

@MarkHerhold
Copy link

@sindresorhus OK, that is fine. This is a major turn-off in terms of adoption of AVA. (#222 does not help either). I looks like the config module lets me define an environment variable called NODE_CONFIG_DIR which should solve the problem. I don't like the idea that my application runs from a different working directory during test as compared to development and production though as it could cause some really nasty and hard to detect bugs.

@jamestalmage
Copy link
Contributor

@MarkHerhold

Currently, the best workaround for achieving the behavior you desire is:

NODE_ENV=test NODE_CONFIG_DIR=$(pwd) ava test/**/*.js

At the top of every test:

process.cwd(process.env.NODE_CONFIG_DIR);

@sindresorhus @vdemedes - Do we want to add a flag or environment variable to address this?

@novemberborn
Copy link
Member

@jamestalmage,

Do we want to add a flag or environment variable to address this?

You mean to change the effective working directory of the test files?

I think generally code should walk up the directory tree looking for the file it wants, or otherwise resolve relative to the package.json (which it needs to find by walking up the tree…). I don't think AVA should change it's behavior.

Closing this now given the lack of discussion over the past three months.

@KidkArolis
Copy link

I ran into this same issue just now. In my mocha tests I always have a mocha.opts --require test/setup config, where in the test/setup.js I could set things such as my NODE_ENV and NODE_CONFIG_DIR. I can't do the same in ava using the ava.require pkg.json config because those get required relatively to each test file...

I wish there was a way to solve this, so that I could just run ava to run my tests without an additional npm script or makefile wrapper. Hmm.

@KidkArolis
Copy link

I suppose in the spirit of being able to just run node foo/test.js, each test file could do a require('../_setup').

On the other hand, that already doesn't work cause babel and ava settings in package.json such as ava.require. So if there was a way of prerequiring a module for each test that is local to the project, that'd be great.

@KidkArolis
Copy link

Made this for now if anyone else runs into this issue.. https://www.npmjs.com/package/ava-config

I can also rename the pkg if ava folk think the name is too confusing.

@sindresorhus
Copy link
Member

config-ava would have been clearer. Now it sounds like it's a config package used internally by AVA, not a AVA version of the config package.

@jamestalmage
Copy link
Contributor

It might be good to make the same request of the community that other projects do. Save the ava- prefix for AVA internal packages (and maybe plugins down the road) and instead use an -ava suffix.

@sindresorhus
Copy link
Member

Yup. Maybe mention it somewhere in the readme.

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

7 participants