-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Needs tests. #5
Comments
Agreed. The current test was designed to be quick and dirty. My focus now is improving the test suite. Forking is done so that the test process's require doesn't get messed up, although that might not be necessary. I think the best way to go about this would be to have one file in |
Let's say I want to write a test confirming the order transforms are applied. Using Nodes module systemI create a fixture module.exports = "foo bar" Then I create two transforms: let transform1 = code => code.replace(/foo/g, 'bar')
let transform2 = code => code.replace(/bar/g, 'baz') I then use pirates to apply 1 and 2 to
That's all fine and good, I have verified my library works. But will I remember all that when I come back in 6 months? 2 years? Will new contributors get it? Another big draw back is that you need to make sure you wipe out the require cache and all applied transforms between each test. I am sure this is why you decided to employ forking. It makes it easier to guarantee a pristine state. With fake-module-systemSame goal. But watch how much easier everything gets. My file contents can just be three characters: var system = new MockSystem();
system.contents['./foo.js'] = 'foo'; Now I add three transforms: pirates.setModuleSystem(system); // or however you decide to inject it
pirates.addTransform(code => code + ' 1');
pirates.addTransform(code => code + '2');
const module = system.load('./foo.js');
console.log(module.code);
That just makes sense, and I can see it all in just a few lines of code. I am not hopping around looking up fixtures. |
OK, @jamestalmage. I've been doing some work on this. You can checkout the I decided to go with mocking out the file system, as using a mock module system seemed wrong to me. Along those lines, I wrote some helpers, that make writing tests really simple and easy. You can find more info on them here. Basically, this is a test: import test from 'ava';
import { makeTest } from './_utils';
test('something', makeTest( // This returns a function, which accepts `t`, then invokes `doTest`.
{
// These are mappings of filenames to content. The filenames a relative to test/fixture, and will get '.js' added
// to the end. Since it is only possible to mock files that exist, it is recommended that you only use the
// names of files in test/fixture, which are currently `foo`, `bar`, `baz`, and `qux`.
foo: 'module.exports = @@a require(\'./bar\');'
bar: 'module.exports = 5 @@b',
},
[
// This is an array of arrays of arguments. pirates.addHook will be invoked with each array as its argument.
// For example, for the following:
code => code.replace('@@a', '\'aa\' + '),
[code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 }],
// Will be converted to:
// pirates.addHook(code => code.replace('@@a', '\'aa\' + '));
// pirates.addHook(code => code.replace('@@b', '* 2'), { matcher: path => path.indexOf('bar') !== -1 });
],
{
// These are mappings of filenames to their expected *export*. Each file will be required and the export
// compared with `t.same` (deep equality). The keys should be the same as above.
foo: '10aa',
bar: 10,
}
)); I'll try to add some more tests this week, although I'm not sure if I'll have a chance. I'll defiantly get some time the week after, though. And you're welcome to contribute some tests, if you'd like. |
As for the code-coverage issue. See how I handled it here: jamestalmage/nyc@d633bbf. Yours would not even need to be that complex. At the top of your tests: var pirates;
if (process.env.PIRATES_COVERAGE) {
pirates = require('../lib/index.covered.js');
require('signal-exit')(function() {
fs.writeFileSync(pathToCoverageOutput, global.__cov__);
});
} else {
pirates = require('../lib/index.js');
} If you want to use
Either method will require use of the |
Ok, I added a few more tests. I think it should be covered pretty well by now, but if there's something missing let me know and I'll add some more. |
Can this issue be closed? Looks like there are tests now. |
Before this can even be considered for adoption, it needs a much more thorough and easy to understand test suite.
If I run into an issue with a library, the first place I look to make sure I am using it correctly is the tests. A single test, that relies on confusing process forking and and pulls in half a dozen fixtures at once just doesn't cut it.
@novemberborn had the great idea of not doing actual source transforms, but simple text once. That is the idea behind
fake-module-system
. I think it makes the tests incapture-require
really easy to understand.The text was updated successfully, but these errors were encountered: