Tengo una función de trabajo que crea un nuevo Blob() en mi aplicación
_buildBlobForProperties(app): Blob { return new Blob( [ JSON.stringify({ name: app.name, description: app.description, }), ], { type: 'application/json', } ); }Y tengo la siguiente prueba:
it('should return properties for update',() => { const blob: Blob = appService._buildBlobForProperties(app); expect(blob.text()).toBe(updateBlob.text()); });Esta prueba funciona bien en Jasmin/Karma pero al migrar la prueba a broma obtengo:
TypeError: blob.text is not a functionY cuando imprimo el contenido del Blob de retorno obtengo
console.log ---> Blob {}¿Alguna sugerencia?
El entorno de prueba de Jest no admite muy bien el objeto Blob , consulte el problema n.º 2555 , le sugiero que use el paquete blob-polyfill para parchear globalmente el objeto Blob en el entorno de prueba. Entonces, ya sea que el entorno de jsdom o un entorno de prueba de node , sus casos de prueba pueden pasar.
service.ts :
export class AppService { _buildBlobForProperties(app): Blob { return new Blob([JSON.stringify({ name: app.name, description: app.description })], { type: 'application/json' }); } } service.test.ts :
import { Blob } from 'blob-polyfill'; import { AppService } from './service'; globalThis.Blob = Blob; describe('69135061', () => { test('should pass', async () => { const appService = new AppService(); const app = { name: 'teresa teng', description: 'best singer' }; const blob: Blob = appService._buildBlobForProperties(app); const text = await blob.text(); expect(text).toEqual(JSON.stringify(app)); }); }); jest.config.js :
module.exports = { preset: 'ts-jest/presets/js-with-ts', testEnvironment: 'jsdom', // testEnvironment: 'node' };resultado de la prueba:
PASS examples/69135061/service.test.ts (8.044 s) 69135061 ✓ should pass (3 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 8.092 s, estimated 9 s Ran all test suites related to changed files.versiones del paquete:
"ts-jest": "^26.4.4", "jest": "^26.6.3", "blob-polyfill": "^5.0.20210201"