mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
Add TS tests tooling (#661)
* install test dependencies * change config for tests * fix linter failing tests * add basic test file * add custom element registration to test * update dependencies * add jest config file * fix test calls on makefile and minor fix on test * update local npm version * clean testm file
This commit is contained in:
@@ -68,7 +68,8 @@ examples:
|
|||||||
test:
|
test:
|
||||||
make examples
|
make examples
|
||||||
npm run build
|
npm run build
|
||||||
$(conda_run) pytest -vv $(ARGS) tests/ --log-cli-level=warning
|
make test-ts
|
||||||
|
make test-py
|
||||||
|
|
||||||
test-local:
|
test-local:
|
||||||
make examples
|
make examples
|
||||||
@@ -81,7 +82,7 @@ test-py:
|
|||||||
|
|
||||||
test-ts:
|
test-ts:
|
||||||
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
|
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
|
||||||
npm run tests
|
npm run test
|
||||||
|
|
||||||
fmt: fmt-py fmt-ts
|
fmt: fmt-py fmt-ts
|
||||||
@echo "Format completed"
|
@echo "Format completed"
|
||||||
|
|||||||
14
pyscriptjs/jest.config.js
Normal file
14
pyscriptjs/jest.config.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
//jest.config.js
|
||||||
|
module.exports = {
|
||||||
|
preset: 'ts-jest',
|
||||||
|
testEnvironment: 'jest-environment-jsdom',
|
||||||
|
globals: {
|
||||||
|
'ts-jest': {
|
||||||
|
tsconfig: 'tsconfig.json'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
verbose: true,
|
||||||
|
testEnvironmentOptions: {
|
||||||
|
url: "http://localhost"
|
||||||
|
}
|
||||||
|
};
|
||||||
8894
pyscriptjs/package-lock.json
generated
8894
pyscriptjs/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,19 +11,24 @@
|
|||||||
"format": "prettier --write './src/**/*.{js,svelte,html,ts}'",
|
"format": "prettier --write './src/**/*.{js,svelte,html,ts}'",
|
||||||
"lint": "eslint './src/**/*.{js,svelte,html,ts}'",
|
"lint": "eslint './src/**/*.{js,svelte,html,ts}'",
|
||||||
"lint:fix": "eslint --fix './src/**/*.{js,svelte,html,ts}'",
|
"lint:fix": "eslint --fix './src/**/*.{js,svelte,html,ts}'",
|
||||||
"xprelint": "npm run format"
|
"xprelint": "npm run format",
|
||||||
|
"test": "jest --coverage",
|
||||||
|
"test:watch": "jest --watch"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^17.0.0",
|
"@rollup/plugin-commonjs": "^17.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^11.0.0",
|
"@rollup/plugin-node-resolve": "^11.0.0",
|
||||||
"@rollup/plugin-typescript": "^8.3.2",
|
"@rollup/plugin-typescript": "^8.3.2",
|
||||||
"@tsconfig/svelte": "^1.0.0",
|
"@tsconfig/svelte": "^1.0.0",
|
||||||
|
"@types/jest": "^28.1.6",
|
||||||
"@types/js-yaml": "^4.0.5",
|
"@types/js-yaml": "^4.0.5",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.20.0",
|
"@typescript-eslint/eslint-plugin": "^5.20.0",
|
||||||
"@typescript-eslint/parser": "^5.20.0",
|
"@typescript-eslint/parser": "^5.20.0",
|
||||||
"autoprefixer": "^10.4.7",
|
"autoprefixer": "^10.4.7",
|
||||||
"eslint": "^8.14.0",
|
"eslint": "^8.14.0",
|
||||||
"eslint-plugin-svelte3": "^3.4.1",
|
"eslint-plugin-svelte3": "^3.4.1",
|
||||||
|
"jest": "^28.1.3",
|
||||||
|
"jest-environment-jsdom": "^28.1.3",
|
||||||
"postcss": "^8.4.13",
|
"postcss": "^8.4.13",
|
||||||
"prettier": "^2.6.2",
|
"prettier": "^2.6.2",
|
||||||
"prettier-plugin-svelte": "^2.7.0",
|
"prettier-plugin-svelte": "^2.7.0",
|
||||||
@@ -38,6 +43,7 @@
|
|||||||
"svelte": "^3.48.0",
|
"svelte": "^3.48.0",
|
||||||
"svelte-check": "^1.0.0",
|
"svelte-check": "^1.0.0",
|
||||||
"svelte-preprocess": "^4.10.6",
|
"svelte-preprocess": "^4.10.6",
|
||||||
|
"ts-jest": "^28.0.7",
|
||||||
"tslib": "^2.4.0",
|
"tslib": "^2.4.0",
|
||||||
"typescript": "^4.6.4"
|
"typescript": "^4.6.4"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -79,16 +79,21 @@ export class BaseEvalElement extends HTMLElement {
|
|||||||
|
|
||||||
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
protected async _register_esm(pyodide: PyodideInterface): Promise<void> {
|
||||||
const imports: { [key: string]: unknown } = {};
|
const imports: { [key: string]: unknown } = {};
|
||||||
|
const nodes = document.querySelectorAll("script[type='importmap']");
|
||||||
for (const node of document.querySelectorAll("script[type='importmap']")) {
|
let importmaps: Array<any>;
|
||||||
|
nodes.forEach( node =>
|
||||||
|
{
|
||||||
let importmap;
|
let importmap;
|
||||||
try {
|
try {
|
||||||
importmap = JSON.parse(node.textContent);
|
importmap = JSON.parse(node.textContent);
|
||||||
if (importmap?.imports == null) continue;
|
if (importmap?.imports == null) return;
|
||||||
|
importmaps.push(importmap);
|
||||||
} catch {
|
} catch {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
for (const importmap of importmaps){
|
||||||
for (const [name, url] of Object.entries(importmap.imports)) {
|
for (const [name, url] of Object.entries(importmap.imports)) {
|
||||||
if (typeof name != 'string' || typeof url != 'string') continue;
|
if (typeof name != 'string' || typeof url != 'string') continue;
|
||||||
|
|
||||||
@@ -116,7 +121,7 @@ export class BaseEvalElement extends HTMLElement {
|
|||||||
: this.getSourceFromElement();
|
: this.getSourceFromElement();
|
||||||
const is_async = source.includes('asyncio')
|
const is_async = source.includes('asyncio')
|
||||||
|
|
||||||
await this._register_esm(runtime);
|
this._register_esm(runtime);
|
||||||
if (is_async) {
|
if (is_async) {
|
||||||
<string>await runtime.runPythonAsync(
|
<string>await runtime.runPythonAsync(
|
||||||
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
|
`output_manager.change(out="${this.outputElement.id}", err="${this.errorElement.id}", append=${this.appendOutput ? 'True' : 'False'})`,
|
||||||
@@ -146,13 +151,15 @@ export class BaseEvalElement extends HTMLElement {
|
|||||||
// check if this REPL contains errors, delete them and remove error classes
|
// check if this REPL contains errors, delete them and remove error classes
|
||||||
const errorElements = document.querySelectorAll(`div[id^='${this.errorElement.id}'][error]`);
|
const errorElements = document.querySelectorAll(`div[id^='${this.errorElement.id}'][error]`);
|
||||||
if (errorElements.length > 0) {
|
if (errorElements.length > 0) {
|
||||||
for (const errorElement of errorElements) {
|
errorElements.forEach( errorElement =>
|
||||||
|
{
|
||||||
errorElement.classList.add('hidden');
|
errorElement.classList.add('hidden');
|
||||||
if (this.hasAttribute('std-err')) {
|
if (this.hasAttribute('std-err')) {
|
||||||
this.errorElement.hidden = true;
|
this.errorElement.hidden = true;
|
||||||
this.errorElement.style.removeProperty('display');
|
this.errorElement.style.removeProperty('display');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
removeClasses(this.errorElement, ['bg-red-200', 'p-2']);
|
removeClasses(this.errorElement, ['bg-red-200', 'p-2']);
|
||||||
|
|
||||||
|
|||||||
16
pyscriptjs/tests/unit/pyrepl.test.ts
Normal file
16
pyscriptjs/tests/unit/pyrepl.test.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import 'jest';
|
||||||
|
import { PyRepl } from '../../src/components/pyrepl';
|
||||||
|
|
||||||
|
customElements.define('py-repl', PyRepl);
|
||||||
|
|
||||||
|
describe('PyRepl', () => {
|
||||||
|
let instance: PyRepl;
|
||||||
|
beforeEach(() => {
|
||||||
|
instance = new PyRepl();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get the current Repl to just instantiate', async () => {
|
||||||
|
expect(instance).toBeInstanceOf(PyRepl);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
@@ -21,10 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
/** Requests the runtime types from the svelte modules by default. Needed for TS files or else you get errors. */
|
/** Requests the runtime types from the svelte modules by default. Needed for TS files or else you get errors. */
|
||||||
"types": ["svelte"],
|
"types": ["svelte", "jest"],
|
||||||
"strict": false,
|
"strict": false,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"forceConsistentCasingInFileNames": true
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"lib": [
|
||||||
|
"es2016",
|
||||||
|
"dom",
|
||||||
|
"DOM.Iterable"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user