Skip to content

Commit

Permalink
pkg
Browse files Browse the repository at this point in the history
- enhance tests
  • Loading branch information
krutoo committed Oct 25, 2023
1 parent a995dfe commit eee8a90
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
dist

# npm pack default output
krutoo-fetch-tools-0.0.0.tgz
33 changes: 12 additions & 21 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"name": "fetch-tools-tests",
"version": "0.0.0",
"description": "Tests",
"private": true,
"scripts": {
"preparing": "npm i --no-save ../krutoo-fetch-tools-0.0.0.tgz",
"test": "npm run test:cjs && npm run test:mjs",
"test:cjs": "node test.cjs",
"test:mjs": "node test.mjs"
},
"dependencies": {
"@krutoo/fetch-tools": "file:.."
}
}
37 changes: 26 additions & 11 deletions test/test.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
const { configureFetch, applyMiddleware } = require('@krutoo/fetch-tools');
const { validateStatus } = require('@krutoo/fetch-tools/middleware');
const fs = require('fs/promises');
const path = require('node:path');
const assert = require('node:assert');

console.assert(typeof configureFetch === 'function');
console.assert(typeof applyMiddleware === 'function');
async function testExports() {
const pkg = JSON.parse(await fs.readFile('../package.json', 'utf-8'));

const myFetch = configureFetch(
fetch,
applyMiddleware(validateStatus(status => status >= 200 && status < 300)),
);
assert.equal(pkg.name, '@krutoo/fetch-tools');

myFetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res => res.json())
.then(console.log);
for (const pathname of Object.keys(pkg.exports)) {
const specifier = path.join(pkg.name, pathname);

assert.doesNotThrow(() => require(specifier));
}
}

async function testBaseFunctionality() {
const { configureFetch, applyMiddleware } = require('@krutoo/fetch-tools');
const { validateStatus } = require('@krutoo/fetch-tools/middleware');

assert.equal(typeof configureFetch, 'function');
assert.equal(typeof applyMiddleware, 'function');

const myFetch = configureFetch(fetch, applyMiddleware(validateStatus(status => status === 200)));

assert.equal(typeof myFetch, 'function');
}

Promise.resolve().then(testExports).then(testBaseFunctionality);
37 changes: 26 additions & 11 deletions test/test.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { configureFetch, applyMiddleware } from '@krutoo/fetch-tools';
import { validateStatus } from '@krutoo/fetch-tools/middleware';
import fs from 'fs/promises';
import path from 'node:path';
import assert from 'node:assert';

console.assert(typeof configureFetch === 'function');
console.assert(typeof applyMiddleware === 'function');
async function testExports() {
const pkg = JSON.parse(await fs.readFile('../package.json', 'utf-8'));

const myFetch = configureFetch(
fetch,
applyMiddleware(validateStatus(status => status >= 200 && status < 300)),
);
assert.equal(pkg.name, '@krutoo/fetch-tools');

myFetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res => res.json())
.then(console.log);
for (const pathname of Object.keys(pkg.exports)) {
const specifier = path.join(pkg.name, pathname);

await assert.doesNotReject(() => import(specifier));
}
}

async function testBaseFunctionality() {
const { configureFetch, applyMiddleware } = await import('@krutoo/fetch-tools');
const { validateStatus } = await import('@krutoo/fetch-tools/middleware');

assert.equal(typeof configureFetch, 'function');
assert.equal(typeof applyMiddleware, 'function');

const myFetch = configureFetch(fetch, applyMiddleware(validateStatus(status => status === 200)));

assert.equal(typeof myFetch, 'function');
}

Promise.resolve().then(testExports).then(testBaseFunctionality);

0 comments on commit eee8a90

Please sign in to comment.