Skip to content

Commit

Permalink
Refactor tests to ESM
Browse files Browse the repository at this point in the history
All tests should be working again
  • Loading branch information
ryanblock committed Mar 2, 2024
1 parent c0c5b41 commit 849a4e3
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 142 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"coverage": "nyc --reporter=lcov --reporter=text npm run test:unit",
"lint": "eslint --fix .",
"test:live": "cross-env tape 'test/live/**/*-test.js' | tap-arc",
"test:plugins": "cross-env tape 'plugins/**/test/**/*-test.*js' | tap-arc",
"test:plugins": "cross-env tape 'plugins/**/test/**/*-test.mjs' | tap-arc",
"test:precommit": "cross-env PRECOMMIT=true npm run gen && npm run lint",
"test:unit": "cross-env tape 'test/unit/**/*-test.js' | tap-arc",
"test:unit": "cross-env tape 'test/unit/**/*-test.mjs' | tap-arc",
"test": "npm run lint && npm run gen && npm run test:plugins && npm run coverage"
},
"simple-git-hooks": {
Expand Down
4 changes: 2 additions & 2 deletions plugins/cloudfront/test/xml-test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import url from 'url'
import { join } from 'path'
import url from 'node:url'
import { join } from 'node:path'
import test from 'tape'
import { readFile } from 'node:fs/promises'
import { parseXML, buildXML } from '../../../src/lib/index.js'
Expand Down
18 changes: 9 additions & 9 deletions plugins/s3/test/s3-test.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
let { join } = require('node:path')
let cwd = process.cwd()
let sut = join(cwd, 'src', 'index.js')
let client = require(sut)
let test = require('tape')
let mockTmp = require('mock-tmp')
let { parseXML } = require('../../../src/lib')
let { defaults } = require('../../../test/lib')
import { join } from 'node:path'
import test from 'tape'
import mockTmp from 'mock-tmp'
import { parseXML } from '../../../src/lib/index.js'
import { defaults } from '../../../test/lib/index.mjs'
let { config } = defaults

let aws, tmp
let aws, client, tmp
let bucketName = 'bucket1'
let objectNames = [ 'object1.txt', 'object2.json' ]
let objectContents = [ 'Hello, World!', JSON.stringify({ welcome: 'aws-lite' }) ]
Expand All @@ -24,6 +21,9 @@ let okXml = {

test('Set up env', async t => {
t.plan(2)
let cwd = process.cwd()
let sut = 'file://' + join(cwd, 'src', 'index.js')
client = (await import(sut)).default
client.testing.enable({ usePluginResponseMethod: true })
aws = await client({ ...config, plugins: [ import('@aws-lite/s3') ] })
t.ok(aws, 'Client ready')
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-plugins/_types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export default async function main ({ service, property, display }) {
const packageName = `@aws-lite/${service}`
const pluginTypesDir = join(CWD, 'plugins', service, 'types')

const { methods } = (await import(join(CWD, 'plugins', service, 'src', 'index.mjs'))).default
const { methods } = (await import('file://' + join(CWD, 'plugins', service, 'src', 'index.mjs'))).default

if (!existsSync(pluginTypesDir)) {
// new plugin types package - this only happens once
Expand Down
9 changes: 5 additions & 4 deletions test/lib/index.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let os = require('node:os')
const http = require('node:http')
import os from 'node:os'
import http from 'node:http'
import process from 'node:process'

// Test defaults
const accessKeyId = 'foo'
Expand Down Expand Up @@ -35,7 +36,7 @@ let server = {
const reqType = req.headers?.['content-type']
/**/ if (reqType?.includes('json')) body = JSON.parse(data)
else if (reqType?.includes('xml')) body = data.toString()
else body = Buffer.concat(data)
else body = data.join('')
}
serverData.request = {
url: req.url,
Expand Down Expand Up @@ -126,7 +127,7 @@ function resetAWSEnvVars () {
delete process.env.AWS_SHARED_CREDENTIALS_FILE
}

module.exports = {
export {
basicRequestChecks,
copy,
defaults,
Expand Down
10 changes: 6 additions & 4 deletions test/unit/plugins/plugins-test.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
let { join } = require('node:path')
let test = require('tape')
import { join } from 'node:path'
import process from 'node:process'
import test from 'tape'

let cwd = process.cwd()
let validTypes = [ 'array', 'boolean', 'buffer', 'number', 'object', 'string' ]
let plugins

test('Set up env', async t => {
t.plan(1)
plugins = (await import('../../../plugins.mjs')).default
plugins = (await import('file://' + join(cwd, 'plugins.mjs'))).default
t.ok(plugins.length, `Loaded ${plugins.length} \`@aws-lite/*\` plugins`)
})

Expand All @@ -15,7 +17,7 @@ test('Check plugins for docs + validation', async t => {
for (let { service } of plugins) {
let prefix = process.platform.startsWith('win') ? 'file://' : ''
let path = service => prefix + join(cwd, 'plugins', service, 'src', 'index.mjs')
let plugin = (await import(path(service))).default
let plugin = (await import('file://' + path(service))).default
t.comment(`@aws-lite/${service}`)

// Traverse methods
Expand Down
20 changes: 11 additions & 9 deletions test/unit/src/config/get-creds-test.mjs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
let { readFileSync } = require('node:fs')
let { join } = require('node:path')
let test = require('tape')
let mockTmp = require('mock-tmp')
let { defaults, overrideHomedir, resetAWSEnvVars } = require('../../../lib')
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import process from 'node:process'
import test from 'tape'
import mockTmp from 'mock-tmp'
import { defaults, overrideHomedir, resetAWSEnvVars } from '../../../lib/index.mjs'

let getCreds
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-creds.js')
let getCreds = require(sut)

let { profile } = defaults
let mock = join(cwd, 'test', 'mock')
let ok = 'foo'
let nope = 'bar'
let num = 1
let credentialsMock = join(mock, '.aws', 'credentials')

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-creds.js')
getCreds = (await import(sut)).default
t.ok(getCreds, 'getCreds module is present')
})

Expand Down
20 changes: 11 additions & 9 deletions test/unit/src/config/get-endpoint-test.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let { readFileSync } = require('node:fs')
let { join } = require('node:path')
let test = require('tape')
let mockTmp = require('mock-tmp')
let { defaults, overrideHomedir, resetAWSEnvVars } = require('../../../lib')
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import test from 'tape'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import { defaults, overrideHomedir, resetAWSEnvVars } from '../../../lib/index.mjs'

let getEndpoint
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-endpoint.js')
let getEndpoint = require(sut)

let { profile } = defaults
let mock = join(cwd, 'test', 'mock')
let configMock = join(mock, '.aws', 'config')
Expand All @@ -18,8 +18,10 @@ let https = 'https:', http = 'http:'
let pathPrefix = '/foo-bar'
let protocol = https // https is the sensible default!

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-endpoint.js')
getEndpoint = (await import(sut)).default
t.ok(getEndpoint, 'getEndpoint module is present')
})

Expand Down
39 changes: 18 additions & 21 deletions test/unit/src/config/get-plugins-test.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
let { readFileSync } = require('node:fs')
let { join } = require('node:path')
let test = require('tape')
let mockTmp = require('mock-tmp')
import { readFileSync } from 'node:fs'
import module from 'node:module'
import { join } from 'node:path'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import test from 'tape'

let getPlugins
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-plugins.js')
let getPlugins = require(sut)

let require = module.createRequire(import.meta.url)
let mock = join(cwd, 'test', 'mock')
let pluginDir = join(mock, 'plugins')

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-plugins.js')
getPlugins = (await import(sut)).default
t.ok(getPlugins, 'getPlugins module is present')
})

Expand All @@ -20,21 +24,14 @@ test('Just return an empty array ', async t => {
})

test('Load plugins array', async t => {
t.plan(2)
t.plan(4)
let plugins

// Node.js 14.x + npm 6 does funky things with npm link-ed (symlinked) modules
// That's cool, we can confidently skip this test for now, the related code path provably works!
if (!process.versions.node.startsWith('14')) {
t.plan(4)
// eslint-disable-next-line
plugins = await getPlugins({ plugins: [ import('@aws-lite/dynamodb') ] })
t.equal(plugins[0].service, 'dynamodb', 'Client explicitly loaded ESM plugin with unresolved import')

// eslint-disable-next-line
plugins = await getPlugins({ plugins: [ await import('@aws-lite/lambda') ] })
t.equal(plugins[0].service, 'lambda', 'Client explicitly loaded ESM plugin with resolved import')
}
plugins = await getPlugins({ plugins: [ import('@aws-lite/dynamodb') ] })
t.equal(plugins[0].service, 'dynamodb', 'Client explicitly loaded ESM plugin with unresolved import')

plugins = await getPlugins({ plugins: [ await import('@aws-lite/lambda') ] })
t.equal(plugins[0].service, 'lambda', 'Client explicitly loaded ESM plugin with resolved import')

let cjsPluginPath = join(pluginDir, 'cjs')
plugins = await getPlugins({ plugins: [ require(cjsPluginPath) ] })
Expand Down
20 changes: 11 additions & 9 deletions test/unit/src/config/get-region-test.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
let { readFileSync } = require('node:fs')
let { join } = require('node:path')
let test = require('tape')
let mockTmp = require('mock-tmp')
let { defaults, overrideHomedir, resetAWSEnvVars } = require('../../../lib')
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import test from 'tape'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import { defaults, overrideHomedir, resetAWSEnvVars } from '../../../lib/index.mjs'

let getRegion
let cwd = process.cwd()
let sut = join(cwd, 'src', 'config', 'get-region.js')
let getRegion = require(sut)

let { profile } = defaults
let mock = join(cwd, 'test', 'mock')
let east1 = 'us-east-1'
Expand All @@ -15,8 +15,10 @@ let west2 = 'us-west-2'
let num = 1
let configMock = join(mock, '.aws', 'config')

test('Set up env', t => {
test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'config', 'get-region.js')
getRegion = (await import(sut)).default
t.ok(getRegion, 'getRegion module is present')
})

Expand Down
23 changes: 14 additions & 9 deletions test/unit/src/index-client-test.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
let { join } = require('node:path')
let { Readable } = require('node:stream')
let qs = require('node:querystring')
let test = require('tape')
let { basicRequestChecks, copy, defaults, resetServer: reset, server } = require('../../lib')
let cwd = process.cwd()
let sut = join(cwd, 'src', 'index.js')
let client = require(sut)

import { join } from 'node:path'
import { Readable } from 'node:stream'
import process from 'node:process'
import qs from 'node:querystring'
import { Buffer } from 'node:buffer'
import test from 'tape'
import { basicRequestChecks, copy, defaults, resetServer as reset, server } from '../../lib/index.mjs'
import url from 'node:url'

let client
let { badPort, config, host, protocol, service, path, port } = defaults
let jsonHeaders = { 'content-type': 'application/json' }
let xmlHeaders = { 'content-type': 'application/xml' }
let __filename = url.fileURLToPath(import.meta.url)

/**
* Reminder!
Expand All @@ -18,6 +20,9 @@ let xmlHeaders = { 'content-type': 'application/xml' }

test('Set up env', async t => {
t.plan(2)
let cwd = process.cwd()
let sut = 'file://' + join(cwd, 'src', 'index.js')
client = (await import(sut)).default
t.ok(client, 'aws-lite client is present')
let started = await server.start()
t.ok(started, 'Started server')
Expand Down
44 changes: 19 additions & 25 deletions test/unit/src/index-config-test.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
let { join } = require('node:path')
let mockTmp = require('mock-tmp')
let test = require('tape')
let { basicRequestChecks, defaults, resetAWSEnvVars: reset, server } = require('../../lib')
import module from 'node:module'
import { join } from 'node:path'
import process from 'node:process'
import mockTmp from 'mock-tmp'
import test from 'tape'
import { basicRequestChecks, defaults, resetAWSEnvVars as reset, server } from '../../lib/index.mjs'

let client
let cwd = process.cwd()
let sut = join(cwd, 'src', 'index.js')
let client = require(sut)

let { accessKeyId, badPort, config, host, path, port, protocol, region, secretAccessKey, service } = defaults
let profile1 = 'profile_1'
let mock = join(cwd, 'test', 'mock')
let pluginDir = join(mock, 'plugins')
let require = module.createRequire(import.meta.url)

test('Set up env', async t => {
t.plan(1)
let sut = 'file://' + join(cwd, 'src', 'index.js')
client = (await import(sut)).default
t.ok(client, 'aws-lite client is present')
})

Expand Down Expand Up @@ -53,22 +57,15 @@ test('Configuration - basic config', async t => {
})

test('Configuration - plugin loading', async t => {
t.plan(6)
t.plan(9)
let aws, tmp
let minConfig = { accessKeyId, secretAccessKey, region }

// Node.js 14.x + npm 6 does funky things with npm link-ed (symlinked) modules
// That's cool, we can confidently skip this test for now, the related code path provably works!
if (!process.versions.node.startsWith('14')) {
t.plan(9)
// eslint-disable-next-line
aws = await client({ ...minConfig, plugins: [ import('@aws-lite/ssm') ] })
t.ok(aws.ssm, 'Client explicitly loaded ESM plugin with unresolved import')

// eslint-disable-next-line
aws = await client({ ...minConfig, plugins: [ await import('@aws-lite/sqs') ] })
t.ok(aws.sqs, 'Client explicitly loaded ESM plugin with resolved import')
}
aws = await client({ ...minConfig, plugins: [ import('@aws-lite/ssm') ] })
t.ok(aws.ssm, 'Client explicitly loaded ESM plugin with unresolved import')

aws = await client({ ...minConfig, plugins: [ await import('@aws-lite/sqs') ] })
t.ok(aws.sqs, 'Client explicitly loaded ESM plugin with resolved import')

let cjsPluginPath = join(pluginDir, 'cjs')
aws = await client({ ...minConfig, plugins: [ require(cjsPluginPath) ] })
Expand All @@ -78,11 +75,8 @@ test('Configuration - plugin loading', async t => {
aws = await client({ ...minConfig, plugins: [ plugin ] })
t.ok(aws.lambda, 'Client explicitly loaded CJS plugin object')

// See above
if (!process.versions.node.startsWith('14')) {
aws = await client({ ...minConfig, autoloadPlugins: true })
t.ok(aws.dynamodb, 'Client auto-loaded @aws-lite/dynamodb')
}
aws = await client({ ...minConfig, autoloadPlugins: true })
t.ok(aws.dynamodb, 'Client auto-loaded @aws-lite/dynamodb')

aws = await client({ ...minConfig })
t.notOk(aws.dynamodb, 'Client did not auto-load @aws-lite/dynamodb')
Expand Down
Loading

0 comments on commit 849a4e3

Please sign in to comment.