diff --git a/lib/index.ts b/lib/index.ts index 6cc52f18..e1f722d8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -7,7 +7,7 @@ import VerboseRenderer from 'listr-verbose-renderer' import { startCase } from 'lodash' import PQueue from 'p-queue' -import { displayErrorLog, setupLogging, writeErrorLogFile } from 'contentful-batch-libs/dist/logging' +import { displayErrorLog, setupLogging, writeErrorLogFile } from 'contentful-batch-libs' import { wrapTask } from 'contentful-batch-libs/dist/listr' import initClient from './tasks/init-client' @@ -16,7 +16,8 @@ import pushToSpace from './tasks/push-to-space/push-to-space' import transformSpace from './transform/transform-space' import { assertDefaultLocale, assertPayload } from './utils/validations' import parseOptions from './parseOptions' -import { ContentfulMultiError, LogItem } from './utils/errors' +import { ContentfulMultiError } from './utils/errors' +import { isDisplayLog, isErrorLog } from './utils/logging' const ONE_SECOND = 1000 @@ -59,7 +60,7 @@ type RunContentfulImportParams = { } async function runContentfulImport (params: RunContentfulImportParams) { - const log: LogItem[] = [] + const log = setupLogging() const options = await parseOptions(params) const listrOptions = createListrOptions(options) const requestQueue = new PQueue({ @@ -69,7 +70,6 @@ async function runContentfulImport (params: RunContentfulImportParams) { }) // Setup custom log listener to store log messages for later - setupLogging(log) const infoTable = new Table() @@ -100,7 +100,6 @@ async function runContentfulImport (params: RunContentfulImportParams) { assertPayload(options.content) } }, - { title: 'Initialize client', task: wrapTask(async (ctx) => { @@ -128,8 +127,7 @@ async function runContentfulImport (params: RunContentfulImportParams) { { title: 'Apply transformations to source data', task: wrapTask(async (ctx) => { - const transformedSourceData = transformSpace(ctx.sourceDataUntransformed, ctx.destinationData) - ctx.sourceData = transformedSourceData + ctx.sourceData = transformSpace(ctx.sourceDataUntransformed, ctx.destinationData) }) }, { @@ -193,8 +191,8 @@ async function runContentfulImport (params: RunContentfulImportParams) { }) }) .then((data) => { - const errorLog = log.filter((logMessage) => logMessage.level !== 'info' && logMessage.level !== 'warning') - const displayLog = log.filter((logMessage) => logMessage.level !== 'info') + const errorLog = log.filter(isErrorLog) + const displayLog = log.filter(isDisplayLog) displayErrorLog(displayLog) if (errorLog.length) { diff --git a/lib/parseOptions.ts b/lib/parseOptions.ts index 3bd6e74d..b62807e4 100644 --- a/lib/parseOptions.ts +++ b/lib/parseOptions.ts @@ -4,8 +4,7 @@ import format from 'date-fns/format' import { version } from '../package.json' import { getHeadersConfig } from './utils/headers' -import { proxyStringToObject, agentFromProxy } from 'contentful-batch-libs/dist/proxy' -import addSequenceHeader from 'contentful-batch-libs/dist/add-sequence-header' +import { addSequenceHeader, proxyStringToObject, agentFromProxy } from 'contentful-batch-libs' import { parseChunked } from '@discoveryjs/json-ext' const SUPPORTED_ENTITY_TYPES = [ diff --git a/lib/tasks/get-destination-data.ts b/lib/tasks/get-destination-data.ts index 9b1692ba..492e9f1d 100644 --- a/lib/tasks/get-destination-data.ts +++ b/lib/tasks/get-destination-data.ts @@ -1,6 +1,6 @@ import Promise from 'bluebird' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' import type { AssetProps, ContentTypeProps, EntryProps, LocaleProps, TagProps, WebhookProps } from 'contentful-management' import { OriginalSourceData } from '../types' import PQueue from 'p-queue' diff --git a/lib/tasks/init-client.ts b/lib/tasks/init-client.ts index 23f3404b..432f5ebf 100644 --- a/lib/tasks/init-client.ts +++ b/lib/tasks/init-client.ts @@ -1,6 +1,6 @@ import { createClient } from 'contentful-management' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' function logHandler (level, data) { logEmitter.emit(level, data) diff --git a/lib/tasks/push-to-space/assets.ts b/lib/tasks/push-to-space/assets.ts index f4203c0b..5b419fa3 100644 --- a/lib/tasks/push-to-space/assets.ts +++ b/lib/tasks/push-to-space/assets.ts @@ -2,9 +2,8 @@ import fs from 'fs' import { join } from 'path' import { promisify } from 'util' -import getEntityName from 'contentful-batch-libs/dist/get-entity-name' -import { logEmitter } from 'contentful-batch-libs/dist/logging' import { ContentfulAssetError, ContentfulEntityError } from '../../utils/errors' +import { getEntityName, logEmitter } from 'contentful-batch-libs' const stat = promisify(fs.stat) @@ -51,11 +50,11 @@ async function lastResult (promises: Promise[]) { } type ProcessAssetsParams = { - assets: any[]; - timeout?: number; - retryLimit?: number; - requestQueue: any; - locales?: string[]; + assets: any[]; + timeout?: number; + retryLimit?: number; + requestQueue: any; + locales?: string[]; }; export async function processAssets ({ diff --git a/lib/tasks/push-to-space/creation.ts b/lib/tasks/push-to-space/creation.ts index a96eaabe..c6254086 100644 --- a/lib/tasks/push-to-space/creation.ts +++ b/lib/tasks/push-to-space/creation.ts @@ -1,8 +1,6 @@ -import { find } from 'lodash/collection' -import { assign, get, omitBy, omit } from 'lodash/object' +import { find, assign, get, omitBy, omit } from 'lodash' -import getEntityName from 'contentful-batch-libs/dist/get-entity-name' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { getEntityName, logEmitter } from 'contentful-batch-libs' import { ContentfulEntityError } from '../../utils/errors' import { TransformedSourceData, TransformedSourceDataUnion } from '../../types' import PQueue from 'p-queue' diff --git a/lib/tasks/push-to-space/publishing.ts b/lib/tasks/push-to-space/publishing.ts index af322da5..af5ed6c8 100644 --- a/lib/tasks/push-to-space/publishing.ts +++ b/lib/tasks/push-to-space/publishing.ts @@ -1,5 +1,4 @@ -import getEntityName from 'contentful-batch-libs/dist/get-entity-name' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter, getEntityName } from 'contentful-batch-libs' import { ContentfulEntityError } from '../../utils/errors' import { ResourcesUnion } from '../../types' import PQueue from 'p-queue' diff --git a/lib/tasks/push-to-space/push-to-space.ts b/lib/tasks/push-to-space/push-to-space.ts index ac2f5717..dd71a5e1 100644 --- a/lib/tasks/push-to-space/push-to-space.ts +++ b/lib/tasks/push-to-space/push-to-space.ts @@ -1,7 +1,7 @@ import Listr from 'listr' import verboseRenderer from 'listr-verbose-renderer' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' import { wrapTask } from 'contentful-batch-libs/dist/listr' import * as assets from './assets' diff --git a/lib/transform/transform-space.ts b/lib/transform/transform-space.ts index 3e588fc3..c2a879a5 100644 --- a/lib/transform/transform-space.ts +++ b/lib/transform/transform-space.ts @@ -1,4 +1,4 @@ -import { omit, defaults } from 'lodash/object' +import { defaults, omit } from 'lodash' import * as defaultTransformers from './transformers' import sortEntries from '../utils/sort-entries' @@ -22,15 +22,14 @@ export default function ( sourceData.locales = sortLocales(sourceData.locales) const tagsEnabled = !!destinationData.tags - return entities.reduce((transformedSpaceData, type) => { + return entities.reduce((transformedSpaceData, type) => { // tags don't contain links to other entities, don't need to be sorted const sortedEntities = (type === 'tags') ? sourceData[type] : sortEntries(sourceData[type]) - const transformedEntities = sortedEntities.map((entity) => ({ + transformedSpaceData[type] = sortedEntities?.map((entity) => ({ original: entity, transformed: transformers[type](entity, destinationData[type], tagsEnabled) })) - transformedSpaceData[type] = transformedEntities return transformedSpaceData }, baseSpaceData) } diff --git a/lib/utils/logging.ts b/lib/utils/logging.ts new file mode 100644 index 00000000..3713d745 --- /dev/null +++ b/lib/utils/logging.ts @@ -0,0 +1,11 @@ +import { LogMessage } from 'contentful-batch-libs' + +type ErrorMessage = Extract + +export function isErrorLog (log: LogMessage): log is ErrorMessage { + return log.level !== 'info' && log.level !== 'warning' +} + +export function isDisplayLog (log: LogMessage): boolean { + return log.level !== 'info' +} diff --git a/lib/utils/sort-entries.ts b/lib/utils/sort-entries.ts index ec123fba..3cc8af4e 100644 --- a/lib/utils/sort-entries.ts +++ b/lib/utils/sort-entries.ts @@ -1,6 +1,4 @@ -import { some, filter, map } from 'lodash/collection' -import * as _o from 'lodash/object' -import { flatten } from 'lodash/array' +import { some, filter, map, flatten, values, get, has } from 'lodash' /** * Given a list of entries, this function reorders them so that entries which @@ -34,7 +32,7 @@ function getLinkedEntries (entries) { const entryIndex = entries.indexOf(entry) const rawLinks = map(entry.fields, (field) => { - field = _o.values(field)[0] + field = values(field)[0] if (isEntryLink(field)) { return getFieldEntriesIndex(field, entries) } else if (isEntityArray(field) && isEntryLink(field[0])) { @@ -50,17 +48,17 @@ function getLinkedEntries (entries) { } function getFieldEntriesIndex (field, entries) { - const id = _o.get(field, 'sys.id') + const id = get(field, 'sys.id') return entries.findIndex((entry) => entry.sys.id === id) } function isEntryLink (item) { - return _o.get(item, 'sys.type') === 'Entry' || - _o.get(item, 'sys.linkType') === 'Entry' + return get(item, 'sys.type') === 'Entry' || + get(item, 'sys.linkType') === 'Entry' } function isEntityArray (item) { - return Array.isArray(item) && item.length > 0 && _o.has(item[0], 'sys') + return Array.isArray(item) && item.length > 0 && has(item[0], 'sys') } /** diff --git a/lib/utils/validations.ts b/lib/utils/validations.ts index 07b58bc6..0c8f9cb4 100644 --- a/lib/utils/validations.ts +++ b/lib/utils/validations.ts @@ -1,5 +1,5 @@ import { payloadSchema } from './schema' -import getEntityName from 'contentful-batch-libs/dist/get-entity-name' +import { getEntityName } from 'contentful-batch-libs' const attachEntityName = (details, payload) => { details.map((detail) => { diff --git a/package-lock.json b/package-lock.json index 9f1cf45a..c2b3a981 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "@discoveryjs/json-ext": "^0.5.7", "bluebird": "^3.7.2", "cli-table3": "^0.6.0", - "contentful-batch-libs": "^9.6.0", + "contentful-batch-libs": "^10.1.1", "contentful-management": "^11.5.0", "date-fns": "^2.30.0", "eslint": "^8.53.0", @@ -4736,14 +4736,13 @@ } }, "node_modules/contentful-batch-libs": { - "version": "9.6.0", - "resolved": "https://registry.npmjs.org/contentful-batch-libs/-/contentful-batch-libs-9.6.0.tgz", - "integrity": "sha512-gGBXBufCltDuxuR8wvN8TPHU3FBdaBt9Jh0KAtbzfQf961woS3mQzWXRA/VjKFT9+OYsOnWNSWoHgdF3JHSYuA==", + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/contentful-batch-libs/-/contentful-batch-libs-10.1.1.tgz", + "integrity": "sha512-T+czEYjS4eTvcrx4GWiGQObowHNPNNaSJQKjjdtXh121+zrv96xj+6ogrWp84BNgi90/9Dyj8EWLq4u5mbynzA==", "dependencies": { "date-fns": "^2.28.0", "figures": "3.2.0", "https-proxy-agent": "^7.0.2", - "lodash.clonedeep": "^4.5.0", "uuid": "^9.0.1" }, "engines": { @@ -9788,10 +9787,6 @@ "integrity": "sha512-kZzYOKspf8XVX5AvmQF94gQW0lejFVgb80G85bU4ZWzoJ6C03PQg3coYAUpSTpQWelrZELd3XWgHzw4Ck5kaIw==", "dev": true }, - "node_modules/lodash.clonedeep": { - "version": "4.5.0", - "license": "MIT" - }, "node_modules/lodash.escaperegexp": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz", @@ -20089,14 +20084,13 @@ } }, "contentful-batch-libs": { - "version": "9.6.0", - "resolved": "https://registry.npmjs.org/contentful-batch-libs/-/contentful-batch-libs-9.6.0.tgz", - "integrity": "sha512-gGBXBufCltDuxuR8wvN8TPHU3FBdaBt9Jh0KAtbzfQf961woS3mQzWXRA/VjKFT9+OYsOnWNSWoHgdF3JHSYuA==", + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/contentful-batch-libs/-/contentful-batch-libs-10.1.1.tgz", + "integrity": "sha512-T+czEYjS4eTvcrx4GWiGQObowHNPNNaSJQKjjdtXh121+zrv96xj+6ogrWp84BNgi90/9Dyj8EWLq4u5mbynzA==", "requires": { "date-fns": "^2.28.0", "figures": "3.2.0", "https-proxy-agent": "^7.0.2", - "lodash.clonedeep": "^4.5.0", "uuid": "^9.0.1" } }, @@ -23285,9 +23279,6 @@ "integrity": "sha512-kZzYOKspf8XVX5AvmQF94gQW0lejFVgb80G85bU4ZWzoJ6C03PQg3coYAUpSTpQWelrZELd3XWgHzw4Ck5kaIw==", "dev": true }, - "lodash.clonedeep": { - "version": "4.5.0" - }, "lodash.escaperegexp": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz", diff --git a/package.json b/package.json index 166d4f4b..08ed7f02 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "@discoveryjs/json-ext": "^0.5.7", "bluebird": "^3.7.2", "cli-table3": "^0.6.0", - "contentful-batch-libs": "^9.6.0", + "contentful-batch-libs": "^10.1.1", "contentful-management": "^11.5.0", "date-fns": "^2.30.0", "eslint": "^8.53.0", diff --git a/test/unit/tasks/init-client.test.ts b/test/unit/tasks/init-client.test.ts index f6a87cff..e8ae096f 100644 --- a/test/unit/tasks/init-client.test.ts +++ b/test/unit/tasks/init-client.test.ts @@ -1,7 +1,7 @@ import initClient from '../../../lib/tasks/init-client' import contentfulManagement from 'contentful-management' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' jest.mock('contentful-management', () => { return { @@ -9,13 +9,8 @@ jest.mock('contentful-management', () => { } }) -jest.mock('contentful-batch-libs/dist/logging', () => { - return { - logEmitter: { - emit: jest.fn() - } - } -}) +jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn()) +const mockedLogEmitter = logEmitter as jest.Mocked test('does create clients and passes custom logHandler', () => { const opts = { @@ -53,6 +48,6 @@ test('does create clients and passes custom logHandler', () => { // Call passed log handler (contentfulManagement.createClient as jest.Mock).mock.calls[0][0].logHandler('level', 'logMessage') - expect(logEmitter.emit.mock.calls[0][0]).toBe('level') - expect(logEmitter.emit.mock.calls[0][1]).toBe('logMessage') + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('level') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('logMessage') }) diff --git a/test/unit/tasks/push/assets.test.ts b/test/unit/tasks/push/assets.test.ts index 68f86d4a..5a0752cf 100644 --- a/test/unit/tasks/push/assets.test.ts +++ b/test/unit/tasks/push/assets.test.ts @@ -6,14 +6,11 @@ import { getAssetStreamForURL } from '../../../../lib/tasks/push-to-space/assets' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' import { MockedFs } from '../../../types' -jest.mock('contentful-batch-libs/dist/logging', () => ({ - logEmitter: { - emit: jest.fn() - } -})) +jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn()) +const mockedLogEmitter = logEmitter as jest.Mocked jest.mock('fs') @@ -31,7 +28,7 @@ beforeEach(() => { interval: 1000, intervalCap: 1000 }) - logEmitter.emit.mockClear(); + mockedLogEmitter.emit.mockClear(); (fs as unknown as MockedFs).__setMockFiles(assetPaths) }) @@ -65,7 +62,7 @@ test('Process assets', async () => { expect(processStub.mock.calls[1][0]).toBe('en-GB') expect(processStub.mock.calls[2][0]).toBe('en-US') expect(processStub.mock.calls[3][0]).toBe('en-GB') - expect(logEmitter.emit.mock.calls).toHaveLength(2) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(2) }) test('Return most up to date processed asset version', async () => { @@ -143,13 +140,13 @@ test('Process assets fails', async () => { // We expect two calls for the first asset (one for each locale) // and two for the second asset of which one fails expect(processStub.mock.calls).toHaveLength(4) - expect(logEmitter.emit.mock.calls).toHaveLength(3) - expect(logEmitter.emit.mock.calls[0][0]).toBe('info') - expect(logEmitter.emit.mock.calls[0][1]).toBe('Processing Asset 123') - expect(logEmitter.emit.mock.calls[1][0]).toBe('info') - expect(logEmitter.emit.mock.calls[1][1]).toBe('Processing Asset 456') - expect(logEmitter.emit.mock.calls[2][0]).toBe('error') - expect(logEmitter.emit.mock.calls[2][1]).toBe(failedError) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(3) + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('Processing Asset 123') + expect(mockedLogEmitter.emit.mock.calls[1][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[1][1]).toBe('Processing Asset 456') + expect(mockedLogEmitter.emit.mock.calls[2][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[2][1]).toBe(failedError) }) test('Get asset stream for url: Throw error if filePath does not exist', async () => { diff --git a/test/unit/tasks/push/creation.test.ts b/test/unit/tasks/push/creation.test.ts index aba6ff20..3b5a86fb 100644 --- a/test/unit/tasks/push/creation.test.ts +++ b/test/unit/tasks/push/creation.test.ts @@ -1,16 +1,14 @@ import PQueue from 'p-queue' import { createEntities, createLocales, createEntries } from '../../../../lib/tasks/push-to-space/creation' -import { logEmitter } from 'contentful-batch-libs/dist/logging' import { ContentfulValidationError } from '../../../../lib/utils/errors' import { EntityTransformed } from '../../../../lib/types' import { LocaleProps, TagProps } from 'contentful-management' +import { logEmitter } from 'contentful-batch-libs' -jest.mock('contentful-batch-libs/dist/logging', () => ({ - logEmitter: { - emit: jest.fn() - } -})) +jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn()) + +const mockedLogEmitter = logEmitter as jest.Mocked let requestQueue @@ -24,7 +22,7 @@ beforeEach(() => { }) afterEach(() => { - logEmitter.emit.mockClear() + mockedLogEmitter.emit.mockClear() }) test('Create entities', () => { @@ -46,8 +44,8 @@ test('Create entities', () => { .then(() => { expect(target.createAssetWithId.mock.calls).toHaveLength(1) expect(updateStub.mock.calls).toHaveLength(1) - expect(logEmitter.emit.mock.calls).toHaveLength(2) - const logLevels = logEmitter.emit.mock.calls.map((args) => args[0]) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(2) + const logLevels = mockedLogEmitter.emit.mock.calls.map((args) => args[0]) expect(logLevels.indexOf('error') !== -1).toBeFalsy() }) }) @@ -77,13 +75,13 @@ test('Create entities handle regular errors', () => { }) .then((result) => { expect(updateStub.mock.calls).toHaveLength(1) - expect(logEmitter.emit.mock.calls).toHaveLength(1) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(1) + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(1) - expect(logEmitter.emit.mock.calls[0][0]).toBe('error') - expect(logEmitter.emit.mock.calls[0][1]).toBe(creationError) + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe(creationError) expect(result).toHaveLength(0) }) }) @@ -112,8 +110,8 @@ test('Create entries', () => { expect(target.createEntryWithId.mock.calls).toHaveLength(1) expect(target.createEntry.mock.calls).toHaveLength(1) expect(updateStub.mock.calls).toHaveLength(1) - expect(logEmitter.emit.mock.calls).toHaveLength(3) - const logLevels = logEmitter.emit.mock.calls.map((args) => args[0]) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(3) + const logLevels = mockedLogEmitter.emit.mock.calls.map((args) => args[0]) expect(logLevels.indexOf('error') !== -1).toBeFalsy() }) }) @@ -154,8 +152,8 @@ test('Create entries and remove unknown fields', () => { expect(updateStub.mock.calls).toHaveLength(2) expect('existingfield' in entries[0].transformed.fields).toBeTruthy() expect('gonefield' in entries[0].transformed.fields).toBeFalsy() - expect(logEmitter.emit.mock.calls).toHaveLength(1) - const logLevels = logEmitter.emit.mock.calls.map((args) => args[0]) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(1) + const logLevels = mockedLogEmitter.emit.mock.calls.map((args) => args[0]) expect(logLevels.indexOf('error') !== -1).toBeFalsy() }) }) @@ -181,13 +179,13 @@ test('Create entries and handle regular errors', () => { }) .then((result) => { expect(updateStub.mock.calls).toHaveLength(1) - expect(logEmitter.emit.mock.calls).toHaveLength(1) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(1) + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(1) - expect(logEmitter.emit.mock.calls[0][0]).toBe('error') - expect(logEmitter.emit.mock.calls[0][1]).toBe(creationError) + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe(creationError) expect(result).toHaveLength(0) }) }) @@ -322,7 +320,7 @@ test('Fails to create locale if it already exists', () => { }) .then((entities) => { expect(entities[0]).toBe(entity) - const logLevels = logEmitter.emit.mock.calls.map((args) => args[0]) + const logLevels = mockedLogEmitter.emit.mock.calls.map((args) => args[0]) expect(logLevels.indexOf('error') !== -1).toBeFalsy() }) }) diff --git a/test/unit/tasks/push/publishing.test.ts b/test/unit/tasks/push/publishing.test.ts index 51224bd8..37c6eac6 100644 --- a/test/unit/tasks/push/publishing.test.ts +++ b/test/unit/tasks/push/publishing.test.ts @@ -4,14 +4,11 @@ import { archiveEntities } from '../../../../lib/tasks/push-to-space/publishing' -import { logEmitter } from 'contentful-batch-libs/dist/logging' +import { logEmitter } from 'contentful-batch-libs' import { AssetProps } from 'contentful-management' -jest.mock('contentful-batch-libs/dist/logging', () => ({ - logEmitter: { - emit: jest.fn() - } -})) +jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn()) +const mockedLogEmitter = logEmitter as jest.Mocked let requestQueue @@ -25,7 +22,7 @@ beforeEach(() => { }) afterEach(() => { - logEmitter.emit.mockClear() + mockedLogEmitter.emit.mockClear() }) test('Publish entities', () => { @@ -42,9 +39,9 @@ test('Publish entities', () => { .then((response) => { expect(publishStub.mock.calls).toHaveLength(2) expect((response[0] as AssetProps).sys.publishedVersion).toBeTruthy() - expect(logEmitter.emit.mock.calls).toHaveLength(4) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(4) + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(0) }) @@ -67,17 +64,17 @@ test('Only publishes valid entities and does not fail when api error occur', () }) .then((result) => { expect(publishStub.mock.calls).toHaveLength(3) - expect(logEmitter.emit.mock.calls[0][0]).toBe('warning') - expect(logEmitter.emit.mock.calls[0][1]).toBe('Unable to publish unknown') - expect(logEmitter.emit.mock.calls[4][0]).toBe('error') - expect(logEmitter.emit.mock.calls[4][1]).toBe(errorValidation) - expect(logEmitter.emit.mock.calls).toHaveLength(7) - const lastLogIndex = logEmitter.emit.mock.calls.length - 1 - expect(logEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') - expect(logEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 2 assets') + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('warning') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('Unable to publish unknown') + expect(mockedLogEmitter.emit.mock.calls[4][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[4][1]).toBe(errorValidation) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(7) + const lastLogIndex = mockedLogEmitter.emit.mock.calls.length - 1 + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 2 assets') expect(result).toHaveLength(2) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(1) expect(errorCount).toBe(1) }) @@ -96,17 +93,17 @@ test('Aborts publishing queue when all publishes fail', () => { }) .then((result) => { expect(publishStub.mock.calls).toHaveLength(2) - expect(logEmitter.emit.mock.calls[4][0]).toBe('error') - expect(logEmitter.emit.mock.calls[4][1]).toBe(errorValidation) - expect(logEmitter.emit.mock.calls).toHaveLength(7) + expect(mockedLogEmitter.emit.mock.calls[4][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[4][1]).toBe(errorValidation) + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(7) expect(result).toHaveLength(0) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(3) - const lastLogIndex = logEmitter.emit.mock.calls.length - 1 - expect(logEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') - expect(logEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 0 assets') + const lastLogIndex = mockedLogEmitter.emit.mock.calls.length - 1 + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 0 assets') }) }) @@ -127,13 +124,13 @@ test('Aborts publishing queue when some publishes fail', () => { .then((result) => { expect(publishStub.mock.calls).toHaveLength(3) expect(result).toHaveLength(1) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(3) - const lastLogIndex = logEmitter.emit.mock.calls.length - 1 - expect(logEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') - expect(logEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 1 assets') + const lastLogIndex = mockedLogEmitter.emit.mock.calls.length - 1 + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Successfully published 1 assets') }) }) @@ -144,14 +141,14 @@ test('Skips publishing when no entities are given', () => { }) .then((result) => { expect(result).toHaveLength(0) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(0) - const lastLogIndex = logEmitter.emit.mock.calls.length - 1 - expect(logEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') - expect(logEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Skipping publishing since zero valid entities passed') - expect(logEmitter.emit.mock.calls).toHaveLength(1) + const lastLogIndex = mockedLogEmitter.emit.mock.calls.length - 1 + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Skipping publishing since zero valid entities passed') + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(1) }) }) @@ -162,14 +159,14 @@ test('Archiving detects entities that can not be archived', () => { }) .then((result) => { expect(result).toHaveLength(0) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(2) expect(errorCount).toBe(0) - const lastLogIndex = logEmitter.emit.mock.calls.length - 1 - expect(logEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') - expect(logEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Skipping archiving since zero valid entities passed') - expect(logEmitter.emit.mock.calls).toHaveLength(3) + const lastLogIndex = mockedLogEmitter.emit.mock.calls.length - 1 + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[lastLogIndex][1]).toBe('Skipping archiving since zero valid entities passed') + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(3) }) }) @@ -198,19 +195,19 @@ test('Skips archiving when no entities are given', () => { .then((result) => { expect(result).toHaveLength(1) expect(result[0]).toMatchObject({ archived: true }) - const warningCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length - const errorCount = logEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length + const warningCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'warning').length + const errorCount = mockedLogEmitter.emit.mock.calls.filter((args) => args[0] === 'error').length expect(warningCount).toBe(0) expect(errorCount).toBe(1) // Init info - expect(logEmitter.emit.mock.calls[0][0]).toBe('info') - expect(logEmitter.emit.mock.calls[0][1]).toBe('Archiving 2 Entrys') + expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('Archiving 2 Entrys') // Error log - expect(logEmitter.emit.mock.calls[1][0]).toBe('error') - expect(logEmitter.emit.mock.calls[1][1]).toBe(errorArchiving) + expect(mockedLogEmitter.emit.mock.calls[1][0]).toBe('error') + expect(mockedLogEmitter.emit.mock.calls[1][1]).toBe(errorArchiving) // Success info - expect(logEmitter.emit.mock.calls[2][0]).toBe('info') - expect(logEmitter.emit.mock.calls[2][1]).toBe('Successfully archived 1 Entrys') - expect(logEmitter.emit.mock.calls).toHaveLength(3) + expect(mockedLogEmitter.emit.mock.calls[2][0]).toBe('info') + expect(mockedLogEmitter.emit.mock.calls[2][1]).toBe('Successfully archived 1 Entrys') + expect(mockedLogEmitter.emit.mock.calls).toHaveLength(3) }) }) diff --git a/test/unit/transform/transform-space.test.ts b/test/unit/transform/transform-space.test.ts index ba187f8f..47652367 100644 --- a/test/unit/transform/transform-space.test.ts +++ b/test/unit/transform/transform-space.test.ts @@ -1,17 +1,11 @@ import { cloneDeep } from 'lodash' -import { - contentTypeMock, - entryMock, - assetMock, - localeMock, - webhookMock -} from 'contentful-batch-libs/test/mocks/' +import { assetMock, contentTypeMock, entryMock, localeMock, webhookMock } from 'contentful-batch-libs/test/mocks/' import transformSpace from '../../../lib/transform/transform-space' import { Resources, TransformedSourceData } from '../../../lib/types' import { TagSysProps } from 'contentful-management/dist/typings/entities/tag' -import type { AssetProps, LocaleProps, WebhookProps } from 'contentful-management' +import type { AssetProps, ContentTypeProps, EntryProps, LocaleProps, WebhookProps } from 'contentful-management' const tagMock = { sys: ({ @@ -21,19 +15,19 @@ const tagMock = { } type ResourcesWithDoNotTouch = Resources & { - doNotTouch?: boolean; + doNotTouch?: boolean; } type TransformedSourceDataWithDoNotTouch = TransformedSourceData & { - doNotTouch?: boolean; + doNotTouch?: boolean; } const space: ResourcesWithDoNotTouch = { - contentTypes: [contentTypeMock], - entries: [entryMock], + contentTypes: [contentTypeMock as unknown as ContentTypeProps], + entries: [entryMock as unknown as EntryProps], assets: [assetMock as unknown as AssetProps], - locales: [localeMock as LocaleProps], - webhooks: [webhookMock as WebhookProps], + locales: [localeMock as unknown as LocaleProps], + webhooks: [webhookMock as unknown as WebhookProps], tags: [tagMock] } const destinationSpace = cloneDeep(space)