From eee8a90b98c186033c4f93ca41bd05a0eaca69c4 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 25 Oct 2023 12:14:19 +0500 Subject: [PATCH] pkg - enhance tests --- .gitignore | 3 +++ test/package-lock.json | 33 ++++++++++++--------------------- test/package.json | 5 +---- test/test.cjs | 37 ++++++++++++++++++++++++++----------- test/test.mjs | 37 ++++++++++++++++++++++++++----------- 5 files changed, 68 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index f06235c..d6c6eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ node_modules dist + +# npm pack default output +krutoo-fetch-tools-0.0.0.tgz diff --git a/test/package-lock.json b/test/package-lock.json index dfd3b38..6a02b4e 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -1,34 +1,25 @@ { - "name": "test", - "version": "1.0.0", + "name": "fetch-tools-tests", + "version": "0.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "test", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "@krutoo/fetch-tools": "file:.." - } + "name": "fetch-tools-tests", + "version": "0.0.0" }, "..": { + "name": "@krutoo/fetch-tools", "version": "0.0.0", + "extraneous": true, "license": "Apache-2.0", "devDependencies": { - "ts-node": "^10.9.1", - "typescript": "^5.0.2" - } - }, - "node_modules/@krutoo/fetch-tools": { - "resolved": "..", - "link": true - } - }, - "dependencies": { - "@krutoo/fetch-tools": { - "version": "file:..", - "requires": { + "@babel/cli": "^7.23.0", + "@babel/core": "^7.23.2", + "@babel/preset-env": "^7.23.2", + "@babel/preset-react": "^7.22.15", + "@babel/preset-typescript": "^7.23.2", + "fs-extra": "^11.1.1", "ts-node": "^10.9.1", "typescript": "^5.0.2" } diff --git a/test/package.json b/test/package.json index da7a3fd..4abd65e 100644 --- a/test/package.json +++ b/test/package.json @@ -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:.." } } diff --git a/test/test.cjs b/test/test.cjs index 1cb10ed..e3eb216 100644 --- a/test/test.cjs +++ b/test/test.cjs @@ -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); diff --git a/test/test.mjs b/test/test.mjs index 16742a3..b728d56 100644 --- a/test/test.mjs +++ b/test/test.mjs @@ -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);