Skip to content

Commit

Permalink
chore(test): enable e2e tests (#1120)
Browse files Browse the repository at this point in the history
* Migrate from Node Test to Vitest
* Use nw to allow reuse of cached binary

Notes: Port the get tests to [nw](https://github.com/nwjs/npm-installer)
  • Loading branch information
ayushmanchhabra committed Jun 15, 2024
1 parent 7f8eb5f commit f802947
Show file tree
Hide file tree
Showing 13 changed files with 940 additions and 580 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: e2e

on:
pull_request:
branches: [main]
branches:
- main

concurrency:
group: ${{ github.ref }}
Expand All @@ -21,13 +22,12 @@ jobs:
steps:
- name: Checkout repository
uses: actions/[email protected]
- name: Get Node version from Node manifest
run: echo "NODE_VER=$(curl -s https://nwjs.io/versions | jq -r ".versions[0].components.node")" >> $GITHUB_ENV
- name: Setup Node
uses: actions/[email protected]
with:
node-version: ${{ env.NODE_VER }}
cache: "npm"
- name: Get Node version from NW.js manifest
uses: volta-cli/[email protected]
- name: Node.js version
run: node -v
- name: npm version
run: npm -v
- name: Install dependencies
run: npm ci
- name: Check for linting errors
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nwjs_build_type=sdk
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ nwbuild({

### Chores

- chore(docs): don't store JSDoc definitions in `typedef`s - get's hard to understand during development.
- chore(get): verify sha checksum for downloads
- chore: annotate file paths as `fs.PathLike` instead of `string`.
- chore(bld): factor out core build step
Expand Down
1,347 changes: 856 additions & 491 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@
"url": "https://github.com/nwutils/nw-builder.git"
},
"scripts": {
"postinstall": "base-volta-off-of-nwjs",
"lint": "eslint ./src/**/*.js ./test/**/*.js",
"lint:fix": "eslint --fix ./src/**/*.js ./test/**/*.js",
"markdown:fix": "markdownlint --fix ./README.md",
"docs": "jsdoc -d docs ./README.md ./src/index.js ./src/get.js ./src/run.js ./src/bld.js",
"test": "vitest run",
"test": "vitest run --coverage",
"demo": "cd test/fixture && node demo.js"
},
"devDependencies": {
"@stylistic/eslint-plugin-js": "^2.1.0",
"@vitest/coverage-v8": "^1.6.0",
"base-volta-off-of-nwjs": "^1.0.5",
"eslint": "^9.4.0",
"eslint-config-tjw-jsdoc": "^1.0.5",
"eslint-plugin-jsdoc": "^48.2.9",
"eslint-plugin-markdown": "^5.0.0",
"jsdoc": "^4.0.3",
"nw": "^0.88.0-3",
"selenium-webdriver": "^4.21.0",
"vitest": "^1.6.0"
},
Expand All @@ -74,5 +78,8 @@
"packageManager": "[email protected]",
"engines": {
"node": ">=14"
},
"volta": {
"node": "22.0.0"
}
}
57 changes: 41 additions & 16 deletions src/get/decompress.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
import fs from "node:fs";
import path from "node:path";
import process from "node:process";

import { beforeAll, describe, it } from "vitest";
import * as nw from "nw";
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";

import decompress from "./decompress.js";
import request from "./request.js";
import util from "../util.js";

describe("get/decompress", function () {
import util from '../util.js';

let tarUrl = "https://dl.nwjs.io/v0.83.0/nwjs-sdk-v0.83.0-linux-x64.tar.gz";
let zipUrl = "https://dl.nwjs.io/v0.83.0/nwjs-sdk-v0.83.0-osx-x64.zip";
describe("get/decompress", async function () {

let nwFilePath = '';
let nwDirPath = '';
let nwOutPath = "./test/fixture/cache";

afterAll(async function () {
await fs.promises.rm(nwOutPath, { recursive: true, force: true });
});

beforeAll(async function () {
const cacheExists = await util.fileExists("./test/fixture/cache")
nwDirPath = await nw.findpath('all', { flavor: 'sdk' });

const cacheExists = await util.fileExists(nwOutPath)
if (!cacheExists) {
await fs.promises.mkdir("./test/fixture/cache");
await fs.promises.mkdir(nwOutPath);
}

await request(tarUrl, "./test/fixture/cache/nw.tar.gz");
await request(zipUrl, "./test/fixture/cache/nw.zip");
}, Infinity);
if (process.platform === 'linux') {
nwFilePath = nwDirPath + '.tar.gz';
} else {
nwFilePath = nwDirPath + '.zip';
}
});

it("decompresses a Linux tarball", async function () {
await decompress("./test/fixture/cache/nw.tar.gz", "./test/fixture/cache");
it("decompresses a NW.js binary", async function () {
await decompress(nwFilePath, nwOutPath);
}, Infinity);

it("decompresses a MacOS zip", async function () {
await decompress("./test/fixture/cache/nw.zip", "./test/fixture/cache");
}, Infinity);
it.runIf(process.platform === 'darwin')("preserves symlinks on macos", async function () {
const frameworksPath = path.resolve(process.cwd(), nwOutPath, nwDirPath, "nwjs.app", "Contents", "Frameworks", "nwjs Framework.framework");
const symlinks = [
path.join(frameworksPath, "Helpers"),
path.join(frameworksPath, "Libraries"),
path.join(frameworksPath, "nwjs Framework"),
path.join(frameworksPath, "Resources"),
path.join(frameworksPath, "Versions", "Current"),
];

for (const symlink of symlinks) {
const stats = await fs.promises.lstat(symlink);
expect(stats.isSymbolicLink()).toEqual(true);
}
});
});
6 changes: 4 additions & 2 deletions src/get/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import axios from "axios";
*
* @async
* @function
*
*
* @param {string} url - Download server
* @param {string} filePath - file path of downloaded content
* @return {Promise<void>}
Expand All @@ -23,5 +23,7 @@ export default async function request(url, filePath) {
responseType: "stream"
});

await stream.promises.pipeline(response.data, writeStream);
response.data.pipe(writeStream);

// await stream.promises.pipeline(response.data, writeStream);
}
12 changes: 3 additions & 9 deletions src/get/request.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import fs from "node:fs";

import { afterEach, describe, expect, it } from "vitest";
import { describe, expect, it } from "vitest";

import util from "../util.js";

import request from "./request.js";

describe("get/request", function () {
describe.skip("get/request", function () {

let url = "https://raw.githubusercontent.com/nwutils/nw-builder/main/src/util/osx.arm.versions.json"
const filePath = "./test/fixture/cache/request.test.json";

afterEach(async function () {
await fs.promises.rm(filePath, { force: true });
});

it("downloads from specific url", async function () {
await request(url, filePath);
expect(util.fileExists(filePath)).resolves.toBe(true);
}, Infinity);
});
});
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ async function fileExists(filePath) {
let exists = true;
try {
await fs.promises.stat(filePath);
} catch {
} catch (_) {
exists = false;
}
return exists;
Expand Down
Empty file.
11 changes: 5 additions & 6 deletions test/specs/bld.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import assert from "node:assert";
import path from "node:path";
import process from "node:process";

import { By } from "selenium-webdriver";
import chrome from "selenium-webdriver/chrome.js";
import { beforeAll, describe, it } from "vitest";
import { beforeAll, describe, expect, it } from "vitest";

import build from "../../src/bld.js";
import get from "../../src/get/index.js";
import util from "../../src/util.js";

const { Driver, ServiceBuilder, Options } = chrome;

describe.skip("build", async () => {
describe.skip("build test suite", async () => {
let driver = undefined;

const nwOptions = {
Expand Down Expand Up @@ -41,11 +40,11 @@ describe.skip("build", async () => {
await get(nwOptions);
}, Infinity);

it("should build without errors", async () => {
it("builds without errors", async () => {
await build(nwOptions);
});

it.skip("should run after build", async () => {
it("runs after build", async () => {
const options = new Options();
const args = [
`--nwapp=${path.resolve("test", "fixture", "app")}`,
Expand All @@ -59,6 +58,6 @@ describe.skip("build", async () => {

driver = Driver.createSession(options, service);
const text = await driver.findElement(By.id("test")).getText();
assert.strictEqual(text, "Hello, World!");
expect(text).toBe("Hello, World!");
}, { timeout: Infinity });
});
46 changes: 0 additions & 46 deletions test/specs/get.test.js

This file was deleted.

12 changes: 12 additions & 0 deletions vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig
} from 'vitest/config'

export default defineConfig
({
test: {
coverage: {
provider: 'v8',
reporter: ['text'],
},
},
})

0 comments on commit f802947

Please sign in to comment.