Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing CJS/ESM; Using GitHub Actions #8

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Testing

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
modern:
name: Node v12-16
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: node tests/test.js
- run: node tests/test.cjs
legacy:
name: Node v6-10
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 10
- 8
- 6
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: node tests/test.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"sideEffects": false,
"description": "The tiniest and the fastest coloring library ever",
"scripts": {
"test": "node test.js"
"test": "node tests/test.js && node tests/test.cjs"
},
"files": [
"picocolors.*"
Expand Down
117 changes: 0 additions & 117 deletions test.js

This file was deleted.

136 changes: 136 additions & 0 deletions tests/suite.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
let assert = require("assert");

const FMT = {
reset: ["\x1b[0m", "\x1b[0m"],
bold: ["\x1b[1m", "\x1b[22m"],
dim: ["\x1b[2m", "\x1b[22m"],
italic: ["\x1b[3m", "\x1b[23m"],
underline: ["\x1b[4m", "\x1b[24m"],
inverse: ["\x1b[7m", "\x1b[27m"],
hidden: ["\x1b[8m", "\x1b[28m"],
strikethrough: ["\x1b[9m", "\x1b[29m"],
black: ["\x1b[30m", "\x1b[39m"],
red: ["\x1b[31m", "\x1b[39m"],
green: ["\x1b[32m", "\x1b[39m"],
yellow: ["\x1b[33m", "\x1b[39m"],
blue: ["\x1b[34m", "\x1b[39m"],
magenta: ["\x1b[35m", "\x1b[39m"],
cyan: ["\x1b[36m", "\x1b[39m"],
white: ["\x1b[37m", "\x1b[39m"],
gray: ["\x1b[90m", "\x1b[39m"],
bgBlack: ["\x1b[40m", "\x1b[49m"],
bgRed: ["\x1b[41m", "\x1b[49m"],
bgGreen: ["\x1b[42m", "\x1b[49m"],
bgYellow: ["\x1b[43m", "\x1b[49m"],
bgBlue: ["\x1b[44m", "\x1b[49m"],
bgMagenta: ["\x1b[45m", "\x1b[49m"],
bgCyan: ["\x1b[46m", "\x1b[49m"],
bgWhite: ["\x1b[47m", "\x1b[49m"],
};

exports.runTestSuite = function runTestSuite(target, pc) {
test("color matching", () => {
for (let format in FMT) {
assert.equal(pc[format]("string"), FMT[format][0] + "string" + FMT[format][1]);
}
});

test("format/color nesting", () => {
assert.equal(
pc.bold(`BOLD ${pc.red(`RED ${pc.dim("DIM")} RED`)} BOLD`),
FMT.bold[0] +
"BOLD " +
FMT.red[0] +
"RED " +
FMT.dim[0] +
"DIM" +
FMT.dim[1] +
FMT.bold[0] +
" RED" +
FMT.red[1] +
" BOLD" +
FMT.bold[1]
);
});

test("proper wrapping", () => {
assert.equal(
pc.red(pc.bold("==TEST==")),
FMT.red[0] + FMT.bold[0] + "==TEST==" + FMT.bold[1] + FMT.red[1]
);
});

test("complex case of wrapping", () => {
assert.equal(
pc.bold(pc.yellow(pc.bgRed(pc.italic("==TEST==")))),
FMT.bold[0] +
FMT.yellow[0] +
FMT.bgRed[0] +
FMT.italic[0] +
"==TEST==" +
FMT.italic[1] +
FMT.bgRed[1] +
FMT.yellow[1] +
FMT.bold[1]
);

assert.equal(
pc.cyan(pc.bold(pc.underline("==TEST=="))),
FMT.cyan[0] +
FMT.bold[0] +
FMT.underline[0] +
"==TEST==" +
FMT.underline[1] +
FMT.bold[1] +
FMT.cyan[1]
);
});

test("close sequence replacement", () => {
assert.equal(
pc.red(`foo ${pc.yellow("bar")} baz`),
FMT.red[0] + "foo " + FMT.yellow[0] + "bar" + FMT.red[0] + " baz" + FMT.red[1]
);

assert.equal(
pc.bold(`foo ${pc.red(pc.dim("bar"))} baz`),
FMT.bold[0] +
"foo " +
FMT.red[0] +
FMT.dim[0] +
"bar" +
FMT.dim[1] +
FMT.bold[0] +
FMT.red[1] +
" baz" +
FMT.bold[1]
);

assert.equal(
pc.yellow(`foo ${pc.red(pc.bold("red"))} bar ${pc.cyan("cyan")} baz`),
FMT.yellow[0] +
"foo " +
FMT.red[0] +
FMT.bold[0] +
"red" +
FMT.bold[1] +
FMT.yellow[0] +
" bar " +
FMT.cyan[0] +
"cyan" +
FMT.yellow[0] +
" baz" +
FMT.yellow[1]
);
});

function test(name, fn) {
try {
fn();
console.log(pc.green("✓ " + target + ": " + name));
} catch (error) {
console.log(pc.red("✗ " + target + ": " + name));
throw error;
}
}
};
4 changes: 4 additions & 0 deletions tests/test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let pc = require("../picocolors.cjs");
let { runTestSuite } = require("./suite.cjs");

runTestSuite("CJS", pc);
4 changes: 4 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as pc from "../picocolors.js";
import { runTestSuite } from "./suite.cjs";

runTestSuite("ESM", pc);