Skip to content

Commit

Permalink
Chore: Update 'ava' to 'v1.1.0'
Browse files Browse the repository at this point in the history
Fix #1597
Close #1720
  • Loading branch information
Jesus David García Gomez authored and molant committed Jan 23, 2019
1 parent f893257 commit 91b1e21
Show file tree
Hide file tree
Showing 141 changed files with 2,518 additions and 2,414 deletions.
2 changes: 1 addition & 1 deletion packages/connector-chrome/package.json
Expand Up @@ -18,7 +18,7 @@
"@types/is-ci": "^1.1.0",
"@types/lockfile": "^1.0.0",
"@types/lodash": "^4.14.120",
"ava": "^0.25.0",
"ava": "^1.1.0",
"cpx": "^1.5.0",
"eslint": "^5.12.1",
"eslint-plugin-import": "^2.15.0",
Expand Down
66 changes: 38 additions & 28 deletions packages/connector-chrome/tests/collect.ts
Expand Up @@ -5,44 +5,54 @@ import * as path from 'path';
import { URL } from 'url';

import * as sinon from 'sinon';
import test, { GenericTestContext, Context } from 'ava';
import anyTest, { TestInterface, ExecutionContext } from 'ava';

import { createServer, ServerConfiguration } from '@hint/utils-create-server';
import { IConnector, IConnectorConstructor } from 'hint/dist/src/lib/types';
import { createServer, ServerConfiguration, Server } from '@hint/utils-create-server';
import { IConnector, Events, IConnectorConstructor } from 'hint/dist/src/lib/types';
import generateHTMLPage from 'hint/dist/src/lib/utils/misc/generate-html-page';
import ChromeConnector from '../src/connector';
import { Engine } from 'hint';

const name: string = 'chrome';

type CollectContext = {
engine: Engine<Events>;
engineEmitSpy: sinon.SinonSpy;
engineEmitAsyncSpy: sinon.SinonSpy;
server: Server;
};

const test = anyTest as TestInterface<CollectContext>;

test.beforeEach(async (t) => {
const engine = {
emit() { },
emitAsync() { }
};
const engine: Engine<Events> = {
emit(): boolean {
return false;
},
async emitAsync(): Promise<any> { }
} as any;

const server = createServer();
const server: Server = createServer();

await server.start();

sinon.spy(engine, 'emit');
sinon.spy(engine, 'emitAsync');
t.context.engineEmitSpy = sinon.spy(engine, 'emit');
t.context.engineEmitAsyncSpy = sinon.spy(engine, 'emitAsync');

t.context = {
engine,
server
};
t.context.server = server;
t.context.engine = engine;
});

test.afterEach.always((t) => {
t.context.server.stop();
t.context.engine.emit.restore();
t.context.engine.emitAsync.restore();
t.context.engineEmitSpy.restore();
t.context.engineEmitAsyncSpy.restore();
});

const pathToFaviconInDir = path.join(__dirname, './fixtures/common/favicon.ico');
const pathToFaviconInLinkElement = path.join(__dirname, './fixtures/common/favicon-32x32.png');

const runTest = async (t: GenericTestContext<Context<any>>, ConnectorConstructor: IConnectorConstructor, serverConfig?: ServerConfiguration) => {
const runTest = async (t: ExecutionContext<CollectContext>, ConnectorConstructor: IConnectorConstructor, serverConfig?: ServerConfiguration) => {
const { engine } = t.context;
const connector: IConnector = new ConnectorConstructor(engine, {});
const server = t.context.server;
Expand All @@ -60,7 +70,7 @@ test(`[${name}] The HTML is downloaded and the right event emitted`, async (t) =

await runTest(t, ChromeConnector, serverConfig);

t.is(t.context.engine.emitAsync.withArgs('fetch::end::html').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::html').callCount, 1);
});

test(`[${name}] Favicon is present in a 'link' element with 'rel' attribute set to 'icon' `, async (t) => {
Expand All @@ -72,8 +82,8 @@ test(`[${name}] Favicon is present in a 'link' element with 'rel' attribute set

await runTest(t, ChromeConnector, serverConfig);

t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').args[0][1].request.url, faviconInLinkElementDir);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').args[0][1].request.url, faviconInLinkElementDir);

});

Expand All @@ -83,8 +93,8 @@ test(`[${name}] Favicon is present in the root directory`, async (t) => {

await runTest(t, ChromeConnector, serverConfig);

t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
});

test(`[${name}] Favicon is present in both the root directory and the 'link' element`, async (t) => {
Expand All @@ -97,9 +107,9 @@ test(`[${name}] Favicon is present in both the root directory and the 'link' ele

await runTest(t, ChromeConnector, serverConfig);

t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').callCount, 1);
// Should load favicon from the link element if it exists
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').args[0][1].request.url, faviconInLinkElementDir);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').args[0][1].request.url, faviconInLinkElementDir);
});

test(`[${name}] Favicon is present in both the root directory and the 'link' element, but the 'link' element has empty 'href'`, async (t) => {
Expand All @@ -111,9 +121,9 @@ test(`[${name}] Favicon is present in both the root directory and the 'link' ele

await runTest(t, ChromeConnector, serverConfig);

t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').callCount, 1);
// Should load favicon from the root even though the link element exists because 'href' is empty.
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
});

test(`[${name}] Favicon is not present in either the root directory or the 'link' element`, async (t) => {
Expand All @@ -123,6 +133,6 @@ test(`[${name}] Favicon is not present in either the root directory or the 'link
await runTest(t, ChromeConnector, serverConfig);

// Requests to `/favicon.ico` are always sent when favicon doesn't exist as a `link` tag in the html.
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engine.emitAsync.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').callCount, 1);
t.is(t.context.engineEmitAsyncSpy.withArgs('fetch::end::image').args[0][1].request.url, faviconInRootDir);
});
35 changes: 23 additions & 12 deletions packages/connector-chrome/tests/evaluate.ts
@@ -1,12 +1,22 @@
import { URL } from 'url';

import test from 'ava';
import anyTest, { TestInterface } from 'ava';

import { createServer } from '@hint/utils-create-server';
import { createServer, Server } from '@hint/utils-create-server';
import generateHTMLPage from 'hint/dist/src/lib/utils/misc/generate-html-page';
import { IConnector } from 'hint/dist/src/lib/types';
import { IConnector, Events } from 'hint/dist/src/lib/types';
import { Engine } from 'hint';

import ChromeConnector from '../src/connector';

type EvaluateContext = {
connector: IConnector;
engine: Engine<Events>;
server: Server;
};

const test = anyTest as TestInterface<EvaluateContext>;

const name: string = 'chrome';

const scripts = [
Expand Down Expand Up @@ -46,20 +56,20 @@ const scripts = [
];

test.beforeEach(async (t) => {
const engine = {
emit() { },
emitAsync() { },
const engine: Engine<Events> = {
emit(): boolean {
return false;
},
async emitAsync(): Promise<any> { },
timeout: 10000
};
} as any;

const server = createServer();

await server.start();

t.context = {
engine,
server
};
t.context.engine = engine;
t.context.server = server;
});

test.afterEach.always(async (t) => {
Expand All @@ -72,9 +82,10 @@ test(`[${name}] Evaluate JavaScript`, async (t) => {
const connector: IConnector = new ChromeConnector(engine, {});
const server = t.context.server;

t.plan(scripts.length);
t.context.connector = connector;

t.plan(scripts.length);

server.configure(generateHTMLPage('', ''));

await connector.collect(new URL(`http://localhost:${server.port}/`));
Expand Down
55 changes: 32 additions & 23 deletions packages/connector-chrome/tests/events.ts
Expand Up @@ -10,14 +10,24 @@ import * as path from 'path';
import { URL } from 'url';

import { map, reduce, groupBy, every } from 'lodash';

import * as sinon from 'sinon';
import test from 'ava';

import anyTest, { TestInterface } from 'ava';
import { createServer, Server } from '@hint/utils-create-server';
import { IConnector } from 'hint/dist/src/lib/types';
import { IConnector, Events } from 'hint/dist/src/lib/types';
import { Engine } from 'hint';

import ChromeConnector from '../src/connector';

type EventsContext = {
connector?: IConnector;
engine: Engine<Events>;
engineEmitSpy: sinon.SinonSpy;
engineEmitAsyncSpy: sinon.SinonSpy;
server: Server;
};

const test = anyTest as TestInterface<EventsContext>;

const name: string = 'chrome';

/* eslint-disable sort-keys */
Expand Down Expand Up @@ -192,31 +202,30 @@ const validEvent = (eventsToSearch: any[], expectedEvent: any) => {
return originalSize !== eventsToSearch.length;
};


test.beforeEach(async (t) => {
const engine = {
emit() { },
emitAsync() { }
};
const engine: Engine<Events> = {
emit(): boolean {
return false;
},
async emitAsync(): Promise<any> { }
} as any;

sinon.spy(engine, 'emitAsync');
sinon.spy(engine, 'emit');
t.context.engineEmitAsyncSpy = sinon.spy(engine, 'emitAsync');
t.context.engineEmitSpy = sinon.spy(engine, 'emit');

const server: Server = createServer();

await server.start();

t.context = {
engine,
server
};
t.context.engine = engine;
t.context.server = server;
});

test.afterEach.always(async (t) => {
t.context.engine.emitAsync.restore();
t.context.engine.emit.restore();
t.context.engineEmitAsyncSpy.restore();
t.context.engineEmitSpy.restore();
t.context.server.stop();
await t.context.connector.close();
await t.context.connector!.close();
});

/**
Expand Down Expand Up @@ -283,15 +292,15 @@ test(`[${name}] Events`, async (t) => {

await connector.collect(new URL(`http://localhost:${server.port}/`));

const { emit, emitAsync } = t.context.engine;
const { engineEmitSpy, engineEmitAsyncSpy } = t.context;
const invokes: any[] = [];

for (let i = 0; i < emitAsync.callCount; i++) {
invokes.push(emitAsync.getCall(i).args);
for (let i = 0; i < engineEmitAsyncSpy.callCount; i++) {
invokes.push(engineEmitAsyncSpy.getCall(i).args);
}

for (let i = 0; i < emit.callCount; i++) {
invokes.push(emit.getCall(i).args);
for (let i = 0; i < engineEmitSpy.callCount; i++) {
invokes.push(engineEmitSpy.getCall(i).args);
}

// List of events that only have to be called once per execution
Expand Down
27 changes: 19 additions & 8 deletions packages/connector-chrome/tests/fetchContent.ts
Expand Up @@ -4,19 +4,30 @@ import * as fs from 'fs';
import * as path from 'path';
import { URL } from 'url';

import test from 'ava';
import anyTest, { TestInterface } from 'ava';
import { createServer, Server } from '@hint/utils-create-server';
import { IConnector, NetworkData, Events } from 'hint/dist/src/lib/types';
import { Engine } from 'hint';

import { createServer } from '@hint/utils-create-server';
import { IConnector, NetworkData } from 'hint/dist/src/lib/types';
import ChromeConnector from '../src/connector';

type FetchContentContext = {
connector?: IConnector;
engine: Engine<Events>;
server: Server;
};

const test = anyTest as TestInterface<FetchContentContext>;

const name: string = 'chrome';

test.beforeEach(async (t) => {
const engine = {
emit() { },
emitAsync() { }
};
const engine: Engine<Events> = {
emit(): boolean {
return false;
},
async emitAsync(): Promise<any> { }
} as any;

const server = createServer();

Expand All @@ -30,7 +41,7 @@ test.beforeEach(async (t) => {

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

test(`[${name}] Fetch Content`, async (t) => {
Expand Down

0 comments on commit 91b1e21

Please sign in to comment.