Skip to content

Commit

Permalink
Try mocha/chai test runners
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Sep 23, 2021
1 parent e007963 commit 61a46bd
Show file tree
Hide file tree
Showing 40 changed files with 877 additions and 1,943 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"devDependencies": {
"@changesets/cli": "^2.16.0",
"@octokit/action": "^3.15.4",
"@types/jest": "^27.0.2",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"del": "^6.0.0",
Expand All @@ -53,7 +52,6 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"execa": "^5.0.0",
"jest": "^27.2.1",
"lerna": "^4.0.0",
"prettier": "^2.4.1",
"tiny-glob": "^0.2.8",
Expand Down
8 changes: 6 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dev": "astro-scripts dev \"src/**/*.ts\"",
"postbuild": "astro-scripts copy \"src/**/*.astro\"",
"benchmark": "node test/benchmark/dev.bench.js && node test/benchmark/build.bench.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
"test": "mocha"
},
"dependencies": {
"@astrojs/compiler": "^0.1.0-canary.45",
Expand Down Expand Up @@ -79,12 +79,16 @@
},
"devDependencies": {
"@types/babel__core": "^7.1.15",
"@types/chai": "^4.2.22",
"@types/connect": "^3.4.35",
"@types/mime": "^2.0.3",
"@types/mocha": "^9.0.0",
"@types/node-fetch": "^2.5.12",
"@types/send": "^0.17.1",
"@types/yargs-parser": "^20.2.1",
"cheerio": "^1.0.0-rc.10"
"chai": "^4.3.4",
"cheerio": "^1.0.0-rc.10",
"mocha": "^9.1.1"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0",
Expand Down
11 changes: 6 additions & 5 deletions packages/astro/test/astro-assets.test.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
/**
* UNCOMMENT: add support for automatic <img> and srcset in build
import { expect } from 'chai';
import { loadFixture } from './test-utils';
let fixture;
beforeAll(async () => {
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-assets/' });
await fixture.build();
});
// TODO: add automatic asset bundling
describe('Assets', () => {
test('built the base image', async () => {
it('built the base image', async () => {
await fixture.readFile('/images/twitter.png');
});
test('built the 2x image', async () => {
it('built the 2x image', async () => {
await fixture.readFile('/images/twitter@2x.png');
});
test('built the 3x image', async () => {
it('built the 3x image', async () => {
await fixture.readFile('/images/twitter@3x.png');
});
});
*/

test.skip('is skipped', () => {});
it.skip('is skipped', () => {});
29 changes: 15 additions & 14 deletions packages/astro/test/astro-attrs.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

let fixture;

beforeAll(async () => {
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-attrs/' });
await fixture.build();
});

describe('Attributes', () => {
test('Passes attributes to elements as expected', async () => {
describe('Attributes', async () => {
it('Passes attributes to elements as expected', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

Expand All @@ -25,32 +26,32 @@ describe('Attributes', () => {

for (const [k, v] of Object.entries(attrs)) {
const attr = $(`#${k}`).attr('attr');
expect(attr).toBe(v);
expect(attr).to.equal(v);
}
});

test('Passes boolean attributes to components as expected', async () => {
it('Passes boolean attributes to components as expected', async () => {
const html = await fixture.readFile('/component/index.html');
const $ = cheerio.load(html);

expect($('#true').attr('attr')).toBe('attr-true');
expect($('#true').attr('type')).toBe('boolean');
expect($('#false').attr('attr')).toBe('attr-false');
expect($('#false').attr('type')).toBe('boolean');
expect($('#true').attr('attr')).to.equal('attr-true');
expect($('#true').attr('type')).to.equal('boolean');
expect($('#false').attr('attr')).to.equal('attr-false');
expect($('#false').attr('type')).to.equal('boolean');
});

test('Passes namespaced attributes as expected', async () => {
it('Passes namespaced attributes as expected', async () => {
const html = await fixture.readFile('/namespaced/index.html');
const $ = cheerio.load(html);

expect($('div').attr('xmlns:happy')).toBe('https://example.com/schemas/happy');
expect($('img').attr('happy:smile')).toBe('sweet');
expect($('div').attr('xmlns:happy')).to.equal('https://example.com/schemas/happy');
expect($('img').attr('happy:smile')).to.equal('sweet');
});

test('Passes namespaced attributes to components as expected', async () => {
it('Passes namespaced attributes to components as expected', async () => {
const html = await fixture.readFile('/namespaced-component/index.html');
const $ = cheerio.load(html);

expect($('span').attr('on:click')).toEqual('(event) => console.log(event)');
expect($('span').attr('on:click')).to.deep.equal('(event) => console.log(event)');
});
});
67 changes: 34 additions & 33 deletions packages/astro/test/astro-basic.test.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,97 @@
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

let fixture;
let previewServer;

beforeAll(async () => {
before(async () => {
fixture = await loadFixture({ projectRoot: './fixtures/astro-basic/' });
await fixture.build();
previewServer = await fixture.preview();
});

describe('Astro basics', () => {
describe('build', () => {
test('Can load page', async () => {
it('Can load page', async () => {
const html = await fixture.readFile(`/index.html`);
const $ = cheerio.load(html);

expect($('h1').text()).toBe('Hello world!');
expect($('h1').text()).to.equal('Hello world!');
});

test('Correctly serializes boolean attributes', async () => {
it('Correctly serializes boolean attributes', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('h1').attr('data-something')).toBe('');
expect($('h2').attr('not-data-ok')).toBe('');
expect($('h1').attr('data-something')).to.equal('');
expect($('h2').attr('not-data-ok')).to.equal('');
});

test('Selector with an empty body', async () => {
it('Selector with an empty body', async () => {
const html = await fixture.readFile('/empty-class/index.html');
const $ = cheerio.load(html);

expect($('.author')).toHaveLength(1);
expect($('.author')).to.have.lengthOf(1);
});

test('Allows forward-slashes in mustache tags (#407)', async () => {
it('Allows forward-slashes in mustache tags (#407)', async () => {
const html = await fixture.readFile('/forward-slash/index.html');
const $ = cheerio.load(html);

expect($('a[href="/post/one"]')).toHaveLength(1);
expect($('a[href="/post/two"]')).toHaveLength(1);
expect($('a[href="/post/three"]')).toHaveLength(1);
expect($('a[href="/post/one"]')).to.have.lengthOf(1);
expect($('a[href="/post/two"]')).to.have.lengthOf(1);
expect($('a[href="/post/three"]')).to.have.lengthOf(1);
});

test('Allows spread attributes (#521)', async () => {
it('Allows spread attributes (#521)', async () => {
const html = await fixture.readFile('/spread/index.html');
const $ = cheerio.load(html);

expect($('#spread-leading')).toHaveLength(1);
expect($('#spread-leading').attr('a')).toBe('0');
expect($('#spread-leading').attr('b')).toBe('1');
expect($('#spread-leading').attr('c')).toBe('2');
expect($('#spread-leading')).to.have.lengthOf(1);
expect($('#spread-leading').attr('a')).to.equal('0');
expect($('#spread-leading').attr('b')).to.equal('1');
expect($('#spread-leading').attr('c')).to.equal('2');

expect($('#spread-trailing')).toHaveLength(1);
expect($('#spread-trailing').attr('a')).toBe('0');
expect($('#spread-trailing').attr('b')).toBe('1');
expect($('#spread-trailing').attr('c')).toBe('2');
expect($('#spread-trailing')).to.have.lengthOf(1);
expect($('#spread-trailing').attr('a')).to.equal('0');
expect($('#spread-trailing').attr('b')).to.equal('1');
expect($('#spread-trailing').attr('c')).to.equal('2');
});

test('Allows spread attributes with TypeScript (#521)', async () => {
it('Allows spread attributes with TypeScript (#521)', async () => {
const html = await fixture.readFile('/spread/index.html');
const $ = cheerio.load(html);

expect($('#spread-ts')).toHaveLength(1);
expect($('#spread-ts').attr('a')).toBe('0');
expect($('#spread-ts').attr('b')).toBe('1');
expect($('#spread-ts').attr('c')).toBe('2');
expect($('#spread-ts')).to.have.lengthOf(1);
expect($('#spread-ts').attr('a')).to.equal('0');
expect($('#spread-ts').attr('b')).to.equal('1');
expect($('#spread-ts').attr('c')).to.equal('2');
});

test('Allows using the Fragment element to be used', async () => {
it('Allows using the Fragment element to be used', async () => {
const html = await fixture.readFile('/fragment/index.html');
const $ = cheerio.load(html);

// will be 1 if element rendered correctly
expect($('#one')).toHaveLength(1);
expect($('#one')).to.have.lengthOf(1);
});
});

describe('preview', () => {
test('returns 200 for valid URLs', async () => {
it('returns 200 for valid URLs', async () => {
const result = await fixture.fetch('/');
expect(result.status).toBe(200);
expect(result.status).to.equal(200);
});

test('returns 404 for invalid URLs', async () => {
it('returns 404 for invalid URLs', async () => {
const result = await fixture.fetch('/bad-url');
expect(result.status).toBe(404);
expect(result.status).to.equal(404);
});
});
});

// important: close preview server (free up port and connection)
afterAll(async () => {
after(async () => {
if (previewServer) await previewServer.stop();
});
41 changes: 21 additions & 20 deletions packages/astro/test/astro-children.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/**
* UNCOMMENT when Component slots lands in new compiler
import { expect } from 'chai';
import cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
let fixture;
beforeAll(async () => {
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-children/',
renderers: ['@astrojs/renderer-preact', '@astrojs/renderer-vue', '@astrojs/renderer-svelte'],
Expand All @@ -16,63 +17,63 @@ beforeAll(async () => {
// TODO: waiting on Component slots
describe('Component children', () => {
test('Passes string children to framework components', async () => {
it('Passes string children to framework components', async () => {
const html = await fixture.readFile('/strings/index.html');
const $ = cheerio.load(html);
// test 1: Can pass text to Preact components
const $preact = $('#preact');
expect($preact.text().trim()).toBe('Hello world');
expect($preact.text().trim()).to.equal('Hello world');
// test 2: Can pass text to Vue components
const $vue = $('#vue');
expect($vue.text().trim()).toBe('Hello world');
expect($vue.text().trim()).to.equal('Hello world');
// test 3: Can pass text to Svelte components
const $svelte = $('#svelte');
expect($svelte.text().trim()).toBe('Hello world');
expect($svelte.text().trim()).to.equal('Hello world');
});
test('Passes markup children to framework components', async () => {
it('Passes markup children to framework components', async () => {
const html = await fixture.readFile('/markup/index.html');
const $ = cheerio.load(html);
// test 1: Can pass markup to Preact components
const $preact = $('#preact h1');
expect($preact.text().trim()).toBe('Hello world');
expect($preact.text().trim()).to.equal('Hello world');
// test 2: Can pass markup to Vue components
const $vue = $('#vue h1');
expect($vue.text().trim()).toBe('Hello world');
expect($vue.text().trim()).to.equal('Hello world');
// test 3: Can pass markup to Svelte components
const $svelte = $('#svelte h1');
expect($svelte.text().trim()).toBe('Hello world');
expect($svelte.text().trim()).to.equal('Hello world');
});
test('Passes multiple children to framework components', async () => {
it('Passes multiple children to framework components', async () => {
const html = await fixture.readFile('/multiple/index.html');
const $ = cheerio.load(html);
// test 1: Can pass multiple children to Preact components
const $preact = $('#preact');
expect($preact.children()).toHaveLength(2);
expect($preact.children(':first-child').text().trim()).toBe('Hello world');
expect($preact.children(':last-child').text().trim()).toBe('Goodbye world');
expect($preact.children()).to.have.lengthOf(2);
expect($preact.children(':first-child').text().trim()).to.equal('Hello world');
expect($preact.children(':last-child').text().trim()).to.equal('Goodbye world');
// test 2: Can pass multiple children to Vue components
const $vue = $('#vue');
expect($vue.children()).toHaveLength(2);
expect($vue.children(':first-child').text().trim()).toBe('Hello world');
expect($vue.children(':last-child').text().trim()).toBe('Goodbye world');
expect($vue.children()).to.have.lengthOf(2);
expect($vue.children(':first-child').text().trim()).to.equal('Hello world');
expect($vue.children(':last-child').text().trim()).to.equal('Goodbye world');
// test 3: Can pass multiple children to Svelte components
const $svelte = $('#svelte');
expect($svelte.children()).toHaveLength(2);
expect($svelte.children(':first-child').text().trim()).toBe('Hello world');
expect($svelte.children(':last-child').text().trim()).toBe('Goodbye world');
expect($svelte.children()).to.have.lengthOf(2);
expect($svelte.children(':first-child').text().trim()).to.equal('Hello world');
expect($svelte.children(':last-child').text().trim()).to.equal('Goodbye world');
});
});
*/

test.skip('is skipped', () => {});
it.skip('is skipped', () => {});
Loading

0 comments on commit 61a46bd

Please sign in to comment.