Does Bitcoin dust limit include miner fee as well? I've read on What is the dust limit on Bitcoin Cash transactions? that small transaction with less than 546 Satoshi get rejected by miners, but I'm confused. 

Is that limited applied to sum of inputs or outputs (excluding miner fee)? 

I'm trying to implement bitcoin testnet wallet on JavaScript, 

async function signTransaction(fromECPair, to, amountToSend) {
    const address = getP2pkhAddress(fromECPair);
    const balance = await getBalance(address);
    const utxo = await getUtxo(address);

    const tx = new Bitcoin.TransactionBuilder(BitcoinNetwork);
    for (let i = 0; i &lt; utxo.length; i++) {
        tx.addInput(utxo[i].txid, utxo[i].vout);
    }

    let transactionFee = (148*utxo.length + 34*2 + 10) * price_per_byte; 
    let amountToKeep = balance - amountToSend - transactionFee;
    tx.addOutput(to, amountToSend);
    tx.addOutput(address, amountToKeep);

    for (let i = 0; i &lt; utxo.length; i++) {
        tx.sign(i, fromECPair);
    }
}


I've just made transaction with one of output less than 546 satoshi so it's definitely possible to make at such output at least on testnet https://chain.so/tx/BTCTEST/021c782fbb75dab2ca843d83a3709e0e846f0d3e99c14a5826f7e18cccfeab25
So attacker can potentially spam my address with lots of tiny outputs and this naive wallet implementation won't work (it would be more costly to sign these UTXOs than value they have). 
What would be the correct way to prevent that?
For a transaction to be standard, Each of the outputs has to be at least 546 satoshis. (except an OP_RETURN output, which has some extra rules).

Testnet does not enforce standardness rules.

You can still include  a nonstandard transaction in your block in mainnet, but still, those transactions are not relayed.