Skip to content

Commit

Permalink
chore(config-tests): refactor config tests (#1102)
Browse files Browse the repository at this point in the history
* chore(config-tests): refactor config tests

* chore(config-tests): adjust tests to pick up .contentfulrc
  • Loading branch information
ruderngespra authored Feb 21, 2023
1 parent 68adcf3 commit 8d38ef4
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions test/unit/bin/lib/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,44 @@ const fileConfig = {
cmaToken: process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN
}

function deleteRequireCache() {
Object.keys(require.cache).forEach((key) => {
delete require.cache[key]
})
}

describe('Config', function () {
beforeEach(function () {
deleteRequireCache()
writeFileSync(resolve('/tmp', '.contentfulrc.json'), JSON.stringify(fileConfig))
})

it('reads the contentfulrc.json', async function () {
it('reads the contentfulrc.json', function () {
const config = getConfig({})
expect(config.accessToken).to.eql(process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN)
})

it('prefers env config over contentfulrc.json', async function () {
it('chooses cmaToken even if managementToken is provided as well', function () {
writeFileSync(
resolve('/tmp', '.contentfulrc.json'),
JSON.stringify({ cmaToken: 'cmaToken', managementToken: 'managementToken' })
)
const config = getConfig({})
expect(config.accessToken).to.eql('cmaToken')
})

// This behavior may be adjusted in the future as some tools which wrap contentful-migration use
// managementToken instead of cmaToken and we may want it to be picked up to ensure compatibility
it('does not pick up managementToken even if no cmaToken is provided', function () {
writeFileSync(
resolve('/tmp', '.contentfulrc.json'),
JSON.stringify({ managementToken: 'managementToken' })
)
const config = getConfig({})
expect(config.accessToken).to.eql(undefined)
})

it('prefers env config over contentfulrc.json', function () {
const token = process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN

process.env.CONTENTFUL_MANAGEMENT_ACCESS_TOKEN = 'schnitzel'
Expand All @@ -27,7 +54,7 @@ describe('Config', function () {
process.env.CONTENTFUL_INTEGRATION_TEST_CMA_TOKEN = token
})

it('prefers handed in config over env config', async function () {
it('prefers handed in config over env config', function () {
const config = getConfig({ accessToken: 'fooMyBar' })
expect(config.accessToken).to.eql('fooMyBar')
})
Expand Down

0 comments on commit 8d38ef4

Please sign in to comment.