How to use the ava.test.beforeEach function in ava

To help you get started, we’ve selected a few ava examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github klazuka / elm-hot / test / setup.js View on Github external
const {startServer} = require('./server.js');

test.before(async () => {
    console.log("Building the Elm code");
    const output = childProcess.execFileSync('./build.sh', {cwd: "./test/fixtures"});
    console.log("Elm build.sh output: " + output);

    global.browser = await puppeteer.launch({
        headless: true,         // default is true; set to false when debugging failed tests
        slowMo: 50,             // introduce a little delay between each operation
        dumpio: false,          // default is false; set to true when debugging failed tests
        args: ['--no-sandbox']  // required for CI builds
    });
});

test.beforeEach(async t => {
    t.context.httpServer = startServer();
    t.context.serverUrl = 'http://127.0.0.1:' + t.context.httpServer.address().port;

    const page = await browser.newPage();
    page.on('pageerror', error => {
        console.log("BROWSER: uncaught exception: " + error);
    });
    page.on('requestfailed', request => {
        console.log("BROWSER: request failed: " + request.url());
    });
    page.on('response', response => {
        if (!response.ok())
            console.error("BROWSER: response: " + response.url() + " " + response.status());
    });
    t.context.page = page;
});
github klazuka / elm-hot / test / example-webpack / setup-webpack.js View on Github external
if (error) {
            console.error(`failed to launch dev server process; error: ${error}`);
        }
    });
}

test.before(async () => {
    global.browser = await puppeteer.launch({
        headless: true,         // default is true; set to false when debugging failed tests
        slowMo: 50,             // introduce a little delay between each operation
        dumpio: false,          // default is false; set to true when debugging failed tests
        args: ['--no-sandbox']  // required for CI builds
    });
});

test.beforeEach(async t => {
    // TODO [kl] if we ever do multiple webpack tests, we will need to randomize the server listen port
    t.context.httpServer = startServer();
    t.context.serverUrl = 'http://127.0.0.1:3333';

    const page = await browser.newPage();

    // TODO [kl] get rid of the brittle sleep. find a better way
    // a one-second delay on my computer is fine, but CircleCI needs more than that.
    console.log("wait for the server to be ready");
    await page.waitFor(5000);
    console.log("done sleeping");

    page.on('pageerror', error => {
        console.log("BROWSER: uncaught exception: " + error);
    });
    page.on('requestfailed', request => {
github berstend / puppeteer-extra / end-to-end-tests / anonymize-ua.js View on Github external
'use strict'

const { test } = require('ava')

test.beforeEach(t => {
  // Make sure we work with pristine modules
  delete require.cache[require.resolve('puppeteer-extra')]
  delete require.cache[require.resolve('puppeteer-extra-plugin-anonymize-ua')]
})

test('will remove headless from the user-agent', async (t) => {
  const puppeteer = require('puppeteer-extra')
  puppeteer.use(require('puppeteer-extra-plugin-anonymize-ua')())

  const browser = await puppeteer.launch({ headless: true })
  const page = await browser.newPage()
  await page.goto('https://httpbin.org/headers', {waitUntil: 'domcontentloaded'})

  const content = await page.content()
  t.true(content.includes('Windows NT 10.0'))
  t.true(!content.includes('HeadlessChrome'))
github webhintio / hint / packages / utils-tests-helpers / src / rule-runner.ts View on Github external
export const testRule = (ruleId: string, ruleTests: Array, configs: { [key: string]: any } = {}) => {
    /**
     * Because tests are executed asynchronously in ava, we need
     * a different server and sonarwhal object for each one
     */
    test.beforeEach(async (t) => {
        // When running serial tests, the server is shared
        if (typeof t.context.server !== 'undefined') {
            return;
        }
        t.context.server = createServer(configs.https);
        await t.context.server.start();
    });

    test.afterEach.always(async (t) => {
        await t.context.server.stop();
    });

    /**
     * Creates a new connector with only the rule to be tested and
     * executing any required `before` task as indicated by `ruleTest`.
     */
github jasongin / nvs / test / modules / linkTests.js View on Github external
const bin = (mockNvsUse.isWindows ? '' : '/bin');
const exe = (mockNvsUse.isWindows ? 'node.exe' : 'node');

function setPath(pathEntries) {
	process.env['PATH'] = pathEntries
		.map(entry => Array.isArray(entry) ? path.join(...entry) : entry)
		.map(mockFs.fixSep)
		.join(path.delimiter);
}

function getPath() {
	return process.env['PATH'].split(path.delimiter);
}

test.beforeEach(t => {
	settings.home = testHome;
	mockNvsUse.isWindows = require('../../lib/use').isWindows;
	mockNvsUse.currentVersion = null;
	mockNvsInstall.mockSystemInstall = false;
	mockWindowsEnv.envMap = {};
	mockWindowsEnv.systemEnvMap = {};

	mockFs.reset();

	mockFs.mockDir(testHome, ['test', 'test2']);
	mockFs.mockDir(path.join(testHome, 'test'), ['5.6.7']);
	mockFs.mockDir(path.join(testHome, 'test', '5.6.7'), ['x86', 'x64']);
	mockFs.mockDir(path.join(testHome, 'test', '5.6.7', 'x86'), []);
	mockFs.mockDir(path.join(testHome, 'test', '5.6.7', 'x64'), []);
	mockFs.mockDir(path.join(testHome, 'test2'), ['5.6.7']);
	mockFs.mockDir(path.join(testHome, 'test2', '5.6.7'), ['x86', 'x64']);
github killercup / wasm-experiments / tests / hello-example.js View on Github external
const { test } = require("ava");
const { instantiate } = require("../src/wasm");
const wrap = require("../src/wrap");

test.beforeEach(async (t) => {
  t.context.i = await instantiate("./hello-wasm.wasm");
});

test(`It's WASM time`, (t) => {
  const time = wrap(t.context.i.exports, "time", [], "CStr");
  t.deepEqual(time(), "Es ist fünf vor Zwölf!!");
});

test(`2 + 2 = 42`, (t) => {
  const add = wrap(t.context.i.exports, "add", ["i32", "i32"], "i32");
  t.deepEqual(add(2, 2), 42);
  t.deepEqual(add(1.1, 1.1), 42);
});

test(`Hello Jan-Erik`, (t) => {
  const hi = wrap(t.context.i.exports, "hi", ["CStr"], "CStr");
github jasongin / nvs / test / modules / migrateTests.js View on Github external
};
nvsMigrate.__set__('nvsList', mockNvsList);

let installPackageCalls = [];
function mockInstallPackage(targetDir, packageName, version) {
	installPackageCalls.push({ targetDir, packageName, version });
}
nvsMigrate.__set__('installPackage', mockInstallPackage);

let linkPackageCalls = [];
function mockLinkPackage(targetDir, packageName, linkTarget, version) {
	linkPackageCalls.push({ targetDir, packageName, linkTarget, version });
}
nvsMigrate.__set__('linkPackage', mockLinkPackage);

test.beforeEach(t => {
	mockFs.reset();
	mockChildProc.reset();
	installPackageCalls = [];
	linkPackageCalls = [];
});

test('Migrate installed module, no version at target', t => {
	mockFs.mockFile(testHome + 'test/5.99.1/x64' + bin + '/' + exe);
	mockFs.mockFile(testHome + 'test/5.99.2/x64' + bin + '/' + exe);
	let modulesPath1 = testHome + 'test/5.99.1/x64' + lib + '/node_modules';
	let modulesPath2 = testHome + 'test/5.99.2/x64' + lib + '/node_modules';
	mockFs.mockDir(modulesPath1, ['test']);
	mockFs.mockDir(modulesPath2, []);
	mockFs.mockDir(path.join(modulesPath1, 'test'), ['package.json']);
	mockFs.mockFile(path.join(modulesPath1, 'test/package.json'),
		JSON.stringify({ version: '1.0.0' }));
github jasongin / nvs / test / modules / addRemoveTests.js View on Github external
const bin = (nvsUse.isWindows ? '' : 'bin');
const exe = (nvsUse.isWindows ? 'node.exe' : 'node');

function setPath(pathEntries) {
	process.env['PATH'] = pathEntries
		.map(entry => Array.isArray(entry) ? path.join(...entry) : entry)
		.map(mockFs.fixSep)
		.join(path.delimiter);
}

function getPath() {
	return process.env['PATH'].split(path.delimiter);
}

test.beforeEach(t => {
	mockFs.reset();
	mockChildProc.reset();
	mockHttp.reset();

	mockFs.mockDir(testHome, ['test1', 'test2']);
	mockFs.mockDir(path.join(testHome, 'test1'), ['5.6.7']);
	mockFs.mockDir(path.join(testHome, 'test1', '5.6.7'), ['x86', 'x64']);
	mockFs.mockDir(path.join(testHome, 'test1', '5.6.7', 'x86'), []);
	mockFs.mockDir(path.join(testHome, 'test1', '5.6.7', 'x64'), []);
	mockFs.mockDir(path.join(testHome, 'test2'), ['6.7.8']);
	mockFs.mockDir(path.join(testHome, 'test2', '6.7.8'), ['x64']);
	mockFs.mockDir(path.join(testHome, 'test2', '6.7.8', 'x64'), []);
	mockFs.mockFile(path.join(testHome, 'test1', '5.6.7', 'x86', bin, exe));
	mockFs.mockFile(path.join(testHome, 'test1', '5.6.7', 'x64', bin, exe));
	mockFs.mockFile(path.join(testHome, 'test2', '6.7.8', 'x64', bin, exe));
	mockHttp.resourceMap['http://example.com/test1/v7.8.9/node-v7.8.9-win-x64.7z'] = 'test';
github jasongin / nvs / test / modules / upgradeTests.js View on Github external
this.linkedVersion = version;
		this.linkCalls.push(version);
		return [ 'Mock linked: ' + version.toString() ];
	},
};
nvsUpgrade.__set__('nvsLink', mockNvsLink);

let mockNvsMigrate = {
	migrateCalls: [],
	migrateGlobalModules(from, to) {
		this.migrateCalls.push([from, to]);
	},
};
nvsUpgrade.__set__('nvsMigrate', mockNvsMigrate);

test.beforeEach(t => {
	mockFs.reset();
	mockNvsList.localVersions = [];
	mockNvsList.remoteVersions = [];
	mockNvsUse.currentVersion = null;
	mockNvsUse.useCalls = [];
	mockNvsAddRemove.addCalls = [];
	mockNvsAddRemove.removeCalls = [];
	mockNvsLink.linkedVersion = null;
	mockNvsLink.linkCalls = [];
	mockNvsMigrate.migrateCalls = [];
});

test.serial('Upgrade to existing version', t => {
	const v1 = NodeVersion.parse('3.4.5');
	const v2 = NodeVersion.parse('3.5.0');
	mockNvsList.localVersions = [ v2, v1 ];
github mobxjs / mobx-state-tree / packages / mobx-state-tree / middleware / simple-action-logger.spec.js View on Github external
const mst = require("..")
const test = require("ava").test
const sinon = require("sinon")

const logger = require("./simple-action-logger").default

test.beforeEach(t => {
    t.context.log = console.log

    console.log = sinon.spy()
})

test.afterEach(t => {
    console.log = t.context.log
})

test("it logs", t => {
    const Todo = mst.types
        .model({
            title: ""
        })
        .actions(self => ({
            helper() {},