-1

I'm new on using AVA for JS unit tests and I immediately hit a rock:

My situation is that I want to run a gulp task to run the AVA tests and watch the test files, and in the test file I wrote I need to include the js file that contains the code to test.

The problem is that the file with the code to test is an old js file with all global functions, so needs to be shimmed somehow into an AMD module, but how I can do this without changing the original file?

gulpfile.js

var gulp = require("gulp");
var ava = require("gulp-ava");

var srcUnitTestFiles = ["**/*.tests.js", "!node_modules/*.js"];

gulp.task("unit-tests-exec", () =>
    gulp.src(srcUnitTestFiles)
        // gulp-ava needs filepaths so you can't have any plugins before it
        .pipe(ava({ verbose: true }))
);

gulp.task("unit-tests-watch", () =>
    gulp.watch(srcUnitTestFiles, ["unit-tests-exec"])
);

package.json

{
  "name": "name",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "ava"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ava": "^0.16.0",
    "gulp": "^3.9.1",
    "gulp-ava": "^0.14.0",
    "jsdom": "^9.4.2"
  },
  "ava": {
    "require": [
      "./test/helpers/setup-browser-env.js"
    ]
  }
}

firstTest.tests.js

import test from "ava";

// I need to import the js file to test

test.before(t => {

});

test("foo", t => {
    t.pass();
});

test('bar', async t => {
    const bar = Promise.resolve('bar');

    t.is(await bar, 'bar');
});

Thanks!

J. Doe
  • 1
  • 1

1 Answers1

0

I think you mean UMD, not AMD. AMD wouldn't work.

I suggest you follow our recipe on browser testing with jsdom.

You could do the following at the top:

global.document = require('jsdom').jsdom('<body></body>');
global.window = document.defaultView;
require('./your-lib');

And then you can access your library on the window global:

window.yourLib();

With yourLib being the method you attached to window in your library.

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
  • Thanks a lot Sindre! I'm able to require the file to test in that way, but actually I think I didn't describe well my situation: the file to test doesn't attach the methods to the Window object, is a file full of plain functions that need to be tested. smth like this is the content of the file: function Name(par) { ... }. So if I require the file to test and I call the function "Name" that I need to test it returns reference error "Name is not defined". I think I need a shim on the file to use it as a module, but how I can do something like that oraccess the function? Thanks a lot! – J. Doe Aug 22 '16 at 10:05
  • P.S: I solved the problem adding code into the js file to test and attaching to a window property "window.tests" the functions that I need to test. If this is the most straightforward solution would be nice to have a gulp plugin that attach all the "orphan" functions of a file to the window object for testing purposes :D I could implement something like that – J. Doe Aug 22 '16 at 10:25