Skip to content

Commit

Permalink
(chore) Convert svelte2tsx tests to typescript (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkine authored Mar 9, 2021
1 parent d29dba0 commit af8af83
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 30 deletions.
1 change: 0 additions & 1 deletion packages/svelte2tsx/mocha.opts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"build": "rollup -c",
"prepublishOnly": "npm run build",
"dev": "rollup -c -w",
"test": "mocha --opts mocha.opts",
"test": "mocha test/test.ts",
"pretest": "rollup -c rollup.config.test.js"
},
"files": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require('fs');
const assert = require('assert');
import fs from 'fs';
import assert from 'assert';

function benchmark(fn) {
export function benchmark(fn) {
return -Date.now() + (fn(), Date.now());
}

function readFileSync(path) {
export function readFileSync(path) {
return fs.existsSync(path)
? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '')
: null;
Expand Down Expand Up @@ -47,7 +47,7 @@ function check_dir(path, { allowed = [], required = allowed }) {
}
}

function test_samples(dir, transform, tsx) {
export function test_samples(dir, transform, tsx) {
for (const testName of fs.readdirSync(`${dir}/samples`)) {
const path = `${dir}/samples/${testName}`;
const expected_path = `${path}/expected.${tsx}`;
Expand Down Expand Up @@ -89,10 +89,8 @@ function test_samples(dir, transform, tsx) {
*
* @param {string} dirPath
*/
function get_input_content(dirPath) {
export function get_input_content(dirPath) {
const filename = fs.readdirSync(dirPath).find((f) => f.endsWith('.svelte'));
const content = readFileSync(`${dirPath}/${filename}`);
return { filename, content };
}

module.exports = { benchmark, test_samples, get_input_content, readFileSync };
6 changes: 0 additions & 6 deletions packages/svelte2tsx/test/htmlx2jsx/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions packages/svelte2tsx/test/htmlx2jsx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { htmlx2jsx } from '../build/htmlxtojsx';
import { test_samples } from '../helpers';

describe('htmlx2jsx', () => {
test_samples(__dirname, htmlx2jsx, 'jsx');
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { htmlx2jsx } = require('../build/htmlxtojsx');
const assert = require('assert');
const { benchmark } = require('../helpers');
import { htmlx2jsx } from '../build/htmlxtojsx';
import assert from 'assert';
import { benchmark } from '../helpers';

describe('htmlxparser', () => {
it('parses in a reasonable time', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
let svelte2tsx = require('../build/index');
let converter = require('../build/htmlxtojsx');
let fs = require('fs');
let assert = require('assert');
let sm = require('source-map');
import svelte2tsx from '../build/index';
import { htmlx2jsx } from '../build/htmlxtojsx';
import fs from 'fs';
import assert from 'assert';
import sm from 'source-map';

describe('sourcemap', () => {
/**
Expand Down Expand Up @@ -95,7 +95,7 @@ describe('sourcemap', () => {
// but assert that the source it generates matches our input source.
//console.log(expected.source)
const { map, code } = dir.endsWith('.htmlx.html')
? converter.htmlx2jsx(expected.source)
? htmlx2jsx(expected.source)
: svelte2tsx(expected.source);
assert.equal(
showWhitespace(code),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const svelte2tsx = require('../build/index');
const { test_samples } = require('../helpers');
import svelte2tsx from '../build/index';
import { test_samples } from '../helpers';

describe('svelte2tsx', () => {
test_samples(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const glob = require('tiny-glob/sync.js');

require('ts-node').register({
project: 'test/tsconfig.json',
transpileOnly: true
});
require('source-map-support').install();
const glob = require('tiny-glob/sync');

//console.clear();

Expand All @@ -9,7 +12,7 @@ if (process.env.CI) {
if (arr.length) throw new Error(`Forgot to remove ".solo" from test(s) ${arr}`);
}

const test_folders = glob('*/index.js', { cwd: 'test' });
const test_folders = glob('*/index.ts', { cwd: 'test' });
const solo_folders = test_folders.filter((folder) => /\.solo$/.test(folder));

if (solo_folders.length) {
Expand Down
10 changes: 10 additions & 0 deletions packages/svelte2tsx/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true
},
"exclude": ["samples/**"],
"include": ["*.ts"]
}

0 comments on commit af8af83

Please sign in to comment.