Skip to content

Expose options object to tests #1057

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

Closed
mmkal opened this issue Sep 30, 2016 · 5 comments
Closed

Expose options object to tests #1057

mmkal opened this issue Sep 30, 2016 · 5 comments
Labels

Comments

@mmkal
Copy link
Contributor

mmkal commented Sep 30, 2016

There may be a way of doing this that I haven't been able to find in the documentation or dicussions of other issues, but I think it'd be nice to be able to access the options object that's used by Api.

Specifically, I'd like to get at the resolveTestsFrom property.
Here's an example of why. NB, this is TypeScript, and I'm using "outDir": "js" in tsconfig.json.

import test from "ava";
import Thing from "../thing";
import fs = require("fs");
import glob = require("glob");

glob.sync("../../**/interesting*.txt").forEach(filename => {
    test(filename + " file should contain hello", t => {
        t.is(fs.readFileSync(filename, "utf8"), "hello");
    });
});

The problem is that ugly ../../ at the beginning of my glob. The number of ../s I have there is coupled to what I set my output directory to in tsconfig.json. In the case of my real project, I end up having to use ../../../ to find what I'm looking for because of my directory structure.

My current workaround is to do this:
in _ava-meta.ts:

const options: { resolveTestsFrom: string } = JSON.parse(process.argv[2]);
export const packageDir = options.resolveTestsFrom;

int thingTest.ts:

import {packageDir} from "../_ava-meta";
...
glob.sync(packageDir + "/**/interesting*.txt");

But it's brittle and a bit of a hack.

My suggestion would be to just expose the options object so I can do:

import test from "ava";
console.log(test.options.resolveTestsFrom); // c:\users\me\myproject

Or is there an already-existing nicer way I can get this value?

@vadimdemedes
Copy link
Contributor

Perhaps you could pass this path as an environment variable to your tests:

$ TESTS_BASE='....' ava

and in your tests:

const base = process.env.TESTS_BASE;

@mmkal
Copy link
Contributor Author

mmkal commented Sep 30, 2016

I could - but that still feels like an unpleasant workaround, and it's probably even more obscure than the one I suggested: it's not immediately obvious (in JavaScript) where that value actually comes from. Is there any reason the options object shouldn't be exposed?

@vadimdemedes
Copy link
Contributor

Actually, this solution is straightforward and clear. process.env is a "standard" way to access environment variables in Node.js apps. AVA proxies all the environment variables you pass to its test processes, so it's totally fine to use that.

Is there any reason the options object shouldn't be exposed?

We try not to add alternatives/aliases or functionality, that already exists in some other form. If we would, AVA could quickly become hard to maintain. That's why we are so picky what goes in the core and what does not. Each change has to be supported in future versions, so we try to avoid mistakes. Hope you understand ;)

Let me know if the process.env solution works for you.

@sindresorhus
Copy link
Member

@mmkal It's seems like what you need is the path of the project root directory (where package.json resides)? If so, in #32 we plan on changing process.cwd() to always be the project root path (where the tests are run from - package.json). When that's changed, you could simply use process.cwd().

@mmkal
Copy link
Contributor Author

mmkal commented Oct 3, 2016

@sindresorhus - great, using process.cwd() would be ideal. I'll close this one since #32 is covering my use case.

@mmkal mmkal closed this as completed Oct 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants