Skip to content

Commit

Permalink
Replace AVA with Vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Dec 13, 2023
1 parent 935374b commit 2ae2787
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 76 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"space": 4
},
"devDependencies": {
"ava": "^6.0.1",
"fetch-mock": "^9.11.0",
"vitest": "^1.0.4",
"xo": "^0.56.0"
},
"engines": {
Expand Down
13 changes: 5 additions & 8 deletions test/fetch-token.js → test/fetch-token.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import test from 'ava';
import { test, assert, beforeEach } from 'vitest';
import fetchMock from 'fetch-mock';
import { refreshTokenURI } from '../index.js';
import getClient from './helpers/get-client.js';

test.beforeEach(t => {
t.context = {
client: getClient(),
};
beforeEach(context => {
context.client = getClient();
});

test('Only returns token from response body', async t => {
const { client } = t.context;
test('Only returns token from response body', async ({ client }) => {
const accessToken = 'access-token';

fetchMock.post(refreshTokenURI, {
access_token: accessToken,
});

t.is(await client.fetchToken(), accessToken);
assert.equal(await client.fetchToken(), accessToken);
});

test.todo('Request includes clientId, and refreshToken');
28 changes: 8 additions & 20 deletions test/get.js → test/get.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import test from 'ava';
import { test, assert, beforeEach } from 'vitest';
import fetchMock from 'fetch-mock';
import getClient from './helpers/get-client.js';

test.beforeEach(t => {
beforeEach(context => {
fetchMock.reset();
t.context = {
client: getClient(),
};
context.client = getClient();
});

test('Get uses default projection when not provided', async t => {
const { client } = t.context;

test('Get uses default projection when not provided', async ({ client }) => {
const mock = fetchMock.getOnce('begin:https://www.googleapis.com', {});
await client.get(undefined, 'token');

t.is(
assert.equal(
mock.lastUrl(),
'https://www.googleapis.com/chromewebstore/v1.1/items/foo?projection=DRAFT',
);
});

test('Get does not fetch token when provided', async t => {
t.plan(0);
const { client } = t.context;

test('Get does not fetch token when provided', async ({ client }) => {
fetchMock.getOnce('begin:https://www.googleapis.com/chromewebstore/v1.1/items/', {});
await client.get(undefined, 'token');
});

test('Get uses token for auth', async t => {
t.plan(0);

const { client } = t.context;
test('Get uses token for auth', async ({ client }) => {
const token = 'token';

fetchMock.getOnce({
Expand All @@ -43,12 +33,10 @@ test('Get uses token for auth', async t => {
await client.get(undefined, token);
});

test('Get uses provided extension ID', async t => {
const { client } = t.context;
test('Get uses provided extension ID', async ({ client }) => {
const { extensionId } = client;

fetchMock.getOnce(`path:/chromewebstore/v1.1/items/${extensionId}`, {});

await client.get(undefined, 'token');
t.pass();
});
31 changes: 8 additions & 23 deletions test/publish.js → test/publish.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
import test from 'ava';
import { test, beforeEach } from 'vitest';
import fetchMock from 'fetch-mock';
import getClient from './helpers/get-client.js';

test.beforeEach(t => {
beforeEach(context => {
fetchMock.reset();
t.context = {
client: getClient(),
};
context.client = getClient();
});

test.serial('Publish uses default target when not provided', async t => {
t.plan(0);
const { client } = t.context;
test('Publish uses default target when not provided', async ({ client }) => {
fetchMock.postOnce('https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=default', {});

await client.publish(undefined, 'token');
});

test.serial('Publish uses target when provided', async t => {
t.plan(0);
const { client } = t.context;
test('Publish uses target when provided', async ({ client }) => {
const target = 'trustedTesters';

fetchMock.postOnce(`https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=${target}`, {});

await client.publish(target, 'token');
});

test.serial('Publish does not fetch token when provided', async t => {
t.plan(0);
const { client } = t.context;

test('Publish does not fetch token when provided', async ({ client }) => {
fetchMock.postOnce('https://www.googleapis.com/chromewebstore/v1.1/items/foo/publish?publishTarget=default', {});

await client.publish(undefined, 'token');
});

test.serial('Publish uses token for auth', async t => {
t.plan(0);

const { client } = t.context;
test('Publish uses token for auth', async ({ client }) => {
const token = 'token';

fetchMock.postOnce({
Expand All @@ -52,10 +40,7 @@ test.serial('Publish uses token for auth', async t => {
await client.publish(undefined, token);
});

test.serial('Uses provided extension ID', async t => {
t.plan(0);

const { client } = t.context;
test('Uses provided extension ID', async ({ client }) => {
const { extensionId } = client;

// Sandbox.stub(got, 'post').callsFake(uri => {
Expand Down
34 changes: 10 additions & 24 deletions test/upload-existing.js → test/upload-existing.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from 'ava';
import { test, assert,expect, beforeEach } from 'vitest';

Check failure on line 1 in test/upload-existing.test.js

View workflow job for this annotation

GitHub Actions / Node.js 20

A space is required after ','.

Check failure on line 1 in test/upload-existing.test.js

View workflow job for this annotation

GitHub Actions / Node.js 18

A space is required after ','.
import fetchMock from 'fetch-mock';
import getClient from './helpers/get-client.js';

Expand All @@ -8,44 +8,33 @@ function stubTokenRequest(token = 'token') {
});
}

test.beforeEach(t => {
beforeEach(context => {
fetchMock.reset();
t.context = {
client: getClient(),
};
context.client = getClient();
});

test.serial('Upload fails when file stream not provided', async t => {
const { client } = t.context;

await t.throwsAsync(client.uploadExisting(), { message: 'Read stream missing' });
test('Upload fails when file stream not provided', async ({ client }) => {
await expect(client.uploadExisting()).rejects.toThrowError('Read stream missing');
});

test.serial('Upload only returns response body on success', async t => {
const { client } = t.context;
test('Upload only returns response body on success', async ({ client }) => {
const body = { foo: 'bar' };

fetchMock.putOnce('https://www.googleapis.com/upload/chromewebstore/v1.1/items/foo', body);

stubTokenRequest();

const response = await client.uploadExisting({});
t.deepEqual(response, body);
assert.deepEqual(response, body);
});

test.serial('Upload does not fetch token when provided', async t => {
const { client } = t.context;

test('Upload does not fetch token when provided', async ({ client }) => {
fetchMock.putOnce('https://www.googleapis.com/upload/chromewebstore/v1.1/items/foo', {});

await client.uploadExisting({}, 'token');
t.pass();
});

test.serial('Upload uses token for auth', async t => {
t.plan(0);

const { client } = t.context;
test('Upload uses token for auth', async ({ client }) => {
const token = 'token';

stubTokenRequest(token);
Expand All @@ -55,10 +44,7 @@ test.serial('Upload uses token for auth', async t => {
await client.uploadExisting({});
});

test.serial('Uses provided extension ID', async t => {
t.plan(0);

const { client } = t.context;
test('Uses provided extension ID', async ({ client }) => {
const { extensionId } = client;

fetchMock.putOnce(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extensionId}`, {
Expand Down

0 comments on commit 2ae2787

Please sign in to comment.