added tests

This commit is contained in:
Simon Haugen 2021-04-22 22:52:14 +02:00
parent 7d7bea7625
commit 6bb34f2cbd
16 changed files with 12955 additions and 191 deletions

47
__tests__/version.js Normal file
View file

@ -0,0 +1,47 @@
const puppeteer = require('puppeteer');
const { setup, teardown } = require('jest-dev-server');
let browser;
let page;
beforeAll(async () => {
browser = await puppeteer.launch();
page = await browser.newPage();
return setup([
{
command: 'npm run demo:dev',
port: 3000,
},
{
command: 'npm run demo:prod',
port: 3001,
},
]);
});
afterAll(async () => {
browser.close();
await teardown();
});
describe('import.meta.env contains PACKAGE_VERSION', () => {
test('in development', async () => {
await page.goto('http://localhost:3000/');
const element = await page.$('#PACKAGE_VERSION');
const version = await element.evaluate((el) => el.textContent);
expect(version).toBe('0.0.0');
});
test('in production', async () => {
await page.goto('http://localhost:3001/');
const element = await page.$('#PACKAGE_VERSION');
const version = await element.evaluate((el) => el.textContent);
expect(version).toBe('0.0.0');
});
});

View file

@ -1,5 +1,5 @@
{ {
"name": "example", "name": "demo",
"version": "0.0.0", "version": "0.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
@ -20,10 +20,15 @@
} }
}, },
"..": { "..": {
"version": "0.0.4", "version": "1.0.0",
"dev": true, "dev": true,
"hasInstallScript": true,
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"jest": "^26.6.3",
"jest-dev-server": "^5.0.0",
"puppeteer": "^9.0.0",
"rimraf": "^3.0.2",
"tsup": "^3.12.1", "tsup": "^3.12.1",
"typescript": "^4.1.5", "typescript": "^4.1.5",
"vite": "^2.0.0-beta.69" "vite": "^2.0.0-beta.69"
@ -1485,6 +1490,10 @@
"vite-plugin-package-version": { "vite-plugin-package-version": {
"version": "file:..", "version": "file:..",
"requires": { "requires": {
"jest": "^26.6.3",
"jest-dev-server": "^5.0.0",
"puppeteer": "^9.0.0",
"rimraf": "^3.0.2",
"tsup": "^3.12.1", "tsup": "^3.12.1",
"typescript": "^4.1.5", "typescript": "^4.1.5",
"vite": "^2.0.0-beta.69" "vite": "^2.0.0-beta.69"

View file

@ -1,10 +1,10 @@
{ {
"name": "example", "name": "demo",
"version": "0.0.0", "version": "0.0.0",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"serve": "vite preview" "serve": "vite preview --port 3001"
}, },
"dependencies": { "dependencies": {
"react": "^17.0.0", "react": "^17.0.0",

View file

@ -1,14 +1,15 @@
import React, { useState } from 'react' import React, { useState } from 'react';
import logo from './logo.svg' import logo from './logo.svg';
import './App.css' import './App.css';
function App() { function App() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0);
return ( return (
<div className="App"> <div className="App">
<header className="App-header"> <header className="App-header">
<img src={logo} className="App-logo" alt="logo" /> <img src={logo} className="App-logo" alt="logo" />
<div id="PACKAGE_VERSION">{import.meta.env.PACKAGE_VERSION}</div>
<p>Hello Vite + React!</p> <p>Hello Vite + React!</p>
<p> <p>
<button onClick={() => setCount((count) => count + 1)}> <button onClick={() => setCount((count) => count + 1)}>
@ -39,7 +40,7 @@ function App() {
</p> </p>
</header> </header>
</div> </div>
) );
} }
export default App export default App;

View file

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -3,8 +3,6 @@ import ReactDOM from 'react-dom';
import './index.css'; import './index.css';
import App from './App'; import App from './App';
console.log(import.meta.env);
ReactDOM.render( ReactDOM.render(
<React.StrictMode> <React.StrictMode>
<App /> <App />

View file

@ -5,4 +5,8 @@ import loadVersion from 'vite-plugin-package-version';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({
plugins: [reactRefresh(), loadVersion()], plugins: [reactRefresh(), loadVersion()],
server: {
port: 3000,
strictPort: true,
},
}); });

4
jest.config.js Normal file
View file

@ -0,0 +1,4 @@
// jest.config.js
module.exports = {
testTimeout: 10000,
};

13011
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -20,16 +20,24 @@
"dist" "dist"
], ],
"scripts": { "scripts": {
"clean": "rm -rf dist", "clean": "rimraf {dist,package-lock.json,node_modules} && npm i",
"build": "npm run clean && tsup src/index.ts --dts", "test": "jest",
"dev": "tsup src/index.ts --dts --watch", "dev": "tsup src/index.ts --dts --watch",
"dev:client": "npm -C example run dev", "build": "rimraf dist && tsup src/index.ts --dts",
"prepublishOnly": "npm run build" "demo:clean": "rimraf demo/{dist,package-lock.json,node_modules} && npm -C demo install",
"demo:dev": "npm -C demo run dev",
"demo:prod": "npm -C demo run build && npm -C demo run serve",
"prepublishOnly": "npm run build && npm run test",
"postinstall": "npm run build"
}, },
"peerDependencies": { "peerDependencies": {
"vite": ">=2.0.0-beta.69" "vite": ">=2.0.0-beta.69"
}, },
"devDependencies": { "devDependencies": {
"jest": "^26.6.3",
"jest-dev-server": "^5.0.0",
"puppeteer": "^9.0.0",
"rimraf": "^3.0.2",
"tsup": "^3.12.1", "tsup": "^3.12.1",
"typescript": "^4.1.5", "typescript": "^4.1.5",
"vite": "^2.0.0-beta.69" "vite": "^2.0.0-beta.69"